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));
}
}
这是我的程序到目前为止,但是我对readPositiveInteger方法有一些问题,我还必须编写一个方法,询问用户是否需要另一个矩形输入“y”如果他想要它并且“n”如果他是不使用while循环。谢谢你
答案 0 :(得分:1)
使用do-while授权您的代码执行次数最少,然后询问用户是否要再次计算结果
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');
答案 1 :(得分:1)
我假设您希望用户通过按Enter确认每个输入
当使用scan.nextInt()
时,这将尝试读取输入中的下一个整数,并保留其中的所有其他内容,这里讨论Using scanner.nextLine()。
我不喜欢跳过不是int的行中的所有内容并阅读第一个内容。因为&#34; hello world number 5&#34;不应该返回5。
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());
}