某人可以編輯我的代碼以使其循環選擇菜單。如果選擇不是5個選項之一,它將提示用戶重新輸入,直到它是一個有效的選項。如果可能的話,解釋也會有幫助。謝謝如何使用循環驗證選擇菜單
這是我的代碼。
import java.util.*;
public class ShapeLoopValidation
{
public static void main (String [] args)
{
chooseShape();
}
public static void chooseShape()
{
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("Select a shape number to calculate area of that shape!");
System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
int shapeChoice = sc.nextInt();
//while (true) {
if (shapeChoice >= 1 && shapeChoice <=4)
{
if (shapeChoice == 1)
{
circle();
}
else if (shapeChoice == 2)
{
rectangle();
}
else if (shapeChoice == 3)
{
triangle();
}
else if (shapeChoice == 4)
{
return;
}
}
else
{
System.out.print("Error : Choice " + shapeChoice + "Does not exist.");
}
}
class Test {
int a, b;
Test(int a, int b) {
this.a = a;
this.b = b;
}
}
}
正確的代碼縮進 – mmuzahid
「有人可以編輯我的代碼,使其循環選擇菜單。如果選擇不是5個選項之一,它將提示用戶重新輸入,直到它是一個有效的選項。「< - 你的代碼已經做到了這一點。什麼問題? –