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,它將執行該方法,但在完成之後,我必須輸入任何值以繼續循環。有任何想法嗎?Java:這段代碼有什麼問題?
嗯什麼 ...什麼是應該做的代碼?請在你的問題中多說一點。通過儘可能完整地填寫問題來幫助我們。 –
這意味着混淆我們! – redFIVE
@redFIVE在這種情況下沒有什麼問題 –