我必须为一个程序编写两种方法,当用户提示宽度和高度时,计算矩形的面积和周长? import java.util.Scanner; 公共类交互矩形 { 公共静态void main(字符串[]args) { int height=readPositiveInteger(“输入高度”); int width=readPositiveInteger(“输入宽度”); 打印矩形细节(高度、宽度); } /** *读入整数并返回其值 *@param要向用户显示的提示 */ 公共静态int readInteger(字符串提示) { 扫描仪扫描=新扫描仪(System.in); System.out.println(提示); while(!scan.hasnetint())//当存在非整数时。。。 { scan.next();/…读取并放弃输入,然后再次提示 System.out.println(“输入错误。输入整数”); } int firstInteger=scan.nextInt(); 返回第一个整数; } /** *读入大于0的整数并返回其值 *@param要向用户显示的提示 */ 公共静态int readPositiveInteger(字符串提示) { int输入=0; 扫描仪扫描=新扫描仪(System.in); 布尔优先=假; 而(!scan.hasnetint()) { int数量=scan.nextInt(); 如果(数量>0) { 第一个=正确; 数量=scan.nextInt(); } 否则,如果(数量

我必须为一个程序编写两种方法,当用户提示宽度和高度时,计算矩形的面积和周长? import java.util.Scanner; 公共类交互矩形 { 公共静态void main(字符串[]args) { int height=readPositiveInteger(“输入高度”); int width=readPositiveInteger(“输入宽度”); 打印矩形细节(高度、宽度); } /** *读入整数并返回其值 *@param要向用户显示的提示 */ 公共静态int readInteger(字符串提示) { 扫描仪扫描=新扫描仪(System.in); System.out.println(提示); while(!scan.hasnetint())//当存在非整数时。。。 { scan.next();/…读取并放弃输入,然后再次提示 System.out.println(“输入错误。输入整数”); } int firstInteger=scan.nextInt(); 返回第一个整数; } /** *读入大于0的整数并返回其值 *@param要向用户显示的提示 */ 公共静态int readPositiveInteger(字符串提示) { int输入=0; 扫描仪扫描=新扫描仪(System.in); 布尔优先=假; 而(!scan.hasnetint()) { int数量=scan.nextInt(); 如果(数量>0) { 第一个=正确; 数量=scan.nextInt(); } 否则,如果(数量,java,loops,if-statement,methods,Java,Loops,If Statement,Methods,用于授权您的代码至少执行一次,然后询问用户是否希望再次计算结果 import java.util.Scanner; public class InteractiveRectangle { public static void main(String[] args) { int height = readPositiveInteger("Enter the height"); int width = readPositiveInteger(

用于授权您的代码至少执行一次,然后询问用户是否希望再次计算结果

import java.util.Scanner;

public class InteractiveRectangle
{
    public static void main(String[] args)
    {

         int height = readPositiveInteger("Enter the height");

         int width = readPositiveInteger("Enter the width");

         printRectangleDetails(height, width);
    }

    /**
     * Read in an integer and return its value
     * @param the prompt to be shown to the user
     */
     public static int readInteger(String prompt)
    {

        Scanner scan = new Scanner(System.in);

        System.out.println(prompt);

        while (!scan.hasNextInt()) // while non-integers are present...
        {
            scan.next(); //...read and discard input, then prompt again

            System.out.println ("Bad input. Enter an integer");
         }

        int firstInteger = scan.nextInt();

        return firstInteger;

    }

    /**
     * Read in an integer greater than 0 and return its value
     * @ param the prompt to be shown to the user
     */
     public static int readPositiveInteger(String prompt)
    {
        int input = 0;

        Scanner scan = new Scanner(System.in);

        boolean first = false;

        while (!scan.hasNextInt())
        {
            int quantity = scan.nextInt();

            if (quantity > 0)
        {
            first = true;

            quantity = scan.nextInt();

        } 
            else if (quantity <= 0)
        {
            System.out.println("Bad input. Enter a positive integer.");
        }

        }
         return input;
   }

     /**
     * Returns the area of a rectangle
     * @param height the height of the rectangle
     * @param width the width of the rectangle
     */

      public static int area (int height, int width)
     {
        return height * width;
      }

     /**
      * Returns the perimeter of a retangle
      * @param height the height of the rectangle
      * @param width the width of the rectangle
      */
      public static int perimeter (int height, int width)
      {
         return (height * 2) + (width * 2);
      }

    /**
     * Prints the area, the perimeter, the length and the width 
     * @param heigth the height of the rectangle
     * @param width the width of the rectangle
     */
     public static void printRectangleDetails (int height, int width)
    {
        System.out.println("The height is " + height);

        System.out.println("The width is " + width);

        System.out.println("The area is " + area(height, width));

        System.out.println("The perimeter is " + perimeter(height, width));
    }
}

我假设您希望用户通过按enter确认每个输入。
使用
scan.nextInt()
时,将尝试读取输入中的下一个整数,并将此处讨论的所有其他内容保留在其中。
我不喜欢跳过行中不是int的所有内容,而是读第一个int,因为“hello world number 5”不应该返回5

char ch = 'y';
do{
int height = readPositiveInteger("Enter the height");
int width = readPositiveInteger("Enter the width");
printRectangleDetails(height, width);
System.out.print("Do you want to calculate other rectangle (y/n) ? : ");
Scanner reader = new Scanner(System.in);
char = reader.next().charAt(0);
}while(ch=='y' || ch=='Y');
publicstaticint-readPositiveInteger(字符串提示){
int输入=0;
扫描仪扫描=新扫描仪(System.in);
while(输入
public static int readPositiveInteger(String prompt) {
    int input = 0;

    Scanner scan = new Scanner(System.in);

    while (input <= 0) {
        // ask for a number
        System.out.println(prompt);
        try { // read the number
            input = scan.nextInt();
        } catch (InputMismatchException e){ // catch if there is no number
            input = 0;
        } // read the remainder of the input line
        scan.nextLine();
    }
    return input;
}

public static boolean anotherRound(){
    Scanner scan = new Scanner(System.in);

    System.out.println("Another Round?");
    // read a full line
    String input = scan.nextLine();
    if("y".equals(input)){ // if "y" then true, else false
        return true;
    }
    return false;
}

public static void main(String[] args) {
    do {
        int posInt = readPositiveInteger("Enter a positive integer");
        System.out.println("You entered " + posInt);
    } while (anotherRound());
}