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 を選択した場合、メソッドは実行されますが、実行後、ループを続行するには値を入力する必要があります。何か案は?