【发布时间】:2016-08-23 03:02:56
【问题描述】:
有人可以编辑我的代码以使其循环选择菜单吗?如果选择不是 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;
}
}
}
【问题讨论】:
-
正确的代码缩进
-
"有人可以编辑我的代码以使其循环选择菜单。如果选择不是 5 个选项之一,它将提示用户重新输入,直到它是一个有效的选项。"
标签: java validation loops