public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
do{
System.out.print("Enter choice:");
int choice;
choice = input.nextInt();
switch (choice)
{
case 1:
FirstProject.areaRectangle();
break;
case 2:
FirstProject.areaTriangle();
break;
default:
System.out.println("lol");
break;
}
}while (input.nextInt()!=0);
}
public static void areaRectangle() {
Scanner input = new Scanner(System.in);
System.out.println("Area of a rectangle.");
System.out.print("Enter the width: ");
double width;
width = input.nextInt();
System.out.print("Enter the height: ");
double height;
height = input.nextInt();
double areaRectangle = (width * height);
System.out.println("The Area of the rectangle is: " + areaRectangle);
}
public static void areaTriangle() {
Scanner input = new Scanner(System.in);
System.out.println("Area of a triangle.");
System.out.print("Enter the base: ");
double base;
base = input.nextInt();
System.out.print("Enter the height: ");
double height;
height = input.nextInt();
double areaTriangle = (base * height) / 2;
System.out.println("The Area of the triangle is: " + areaTriangle);
}
}
那是我的代码并且它有效,唯一困扰我的是我必须输入除“0”之外的任何值才能保持循环。例如,如果我选择案例 1,它将执行该方法,但在执行此操作之后,我必须输入任何值才能继续循环。有任何想法吗?