假設我之前測驗過的這些方法有效,我可以要求第二組眼睛看看我在這個 While 回圈中做錯了什么嗎?我正在尋找它重復形狀,直到用戶專門輸入 0。如文本所示,我正在做圓形、三角形和矩形。
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an integer representing the shape would you like the area of.n/ (1-Triangle, 2-Circle, 3-Rectangle)");
int shape = scanner.nextInt();
do{
//dowhile loop
int shapecheck=1;
switch(shape)
{
case 1:CalcArea.CalcTri();
System.out.println("Another shape? (1-yes, 0-no)");
int shapecheck = scanner.nextInt();
break;
case 2:CalcArea.CalcCir();
System.out.println("Another shape? (1-yes, 0-no)");
int shapecheck = scanner.nextInt();
break;
case 3:CalcArea.CalcRect();
System.out.println("Another shape? (1-yes, 0-no)");
int shapecheck = scanner.nextInt();
break;
default:
shapecheck = 0;
}
}while (shapecheck==1);
}
uj5u.com熱心網友回復:
將宣告shapecheck移到回圈外,使其在回圈條件中可見。不要在案例中重新宣告變數;而是分配給它。
int shapecheck = 1;
do {
switch (shape) {
case 1:
CalcArea.CalcTri();
System.out.println("Another shape? (1-yes, 0-no)");
shapecheck = scanner.nextInt();
break;
case 2:
CalcArea.CalcCir();
System.out.println("Another shape? (1-yes, 0-no)");
shapecheck = scanner.nextInt();
break;
case 3:
CalcArea.CalcRect();
System.out.println("Another shape? (1-yes, 0-no)");
shapecheck = scanner.nextInt();
break;
default:
shapecheck = 0;
}
} while (shapecheck == 1);
uj5u.com熱心網友回復:
我認為在回圈中你不會向用戶詢問形狀變數,所以它保持在初始輸入的值。您只詢問用戶是否希望輸入另一個值,而不是值本身。再次scanner.nextInt()詢問形狀
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/515409.html
標籤:爪哇调试审查
