Java 强制用户在选项2之前选择选项1
我想强制用户在继续之前选择选项1,然后选择选项2。任何帮助都将不胜感激。 (注意,到目前为止,我只在java中完成了if、switch、while、for、do…while、方法和数组)Java 强制用户在选项2之前选择选项1,java,Java,我想强制用户在继续之前选择选项1,然后选择选项2。任何帮助都将不胜感激。 (注意,到目前为止,我只在java中完成了if、switch、while、for、do…while、方法和数组) 在至少选择一次1之前,您无法显示选项2 Scanner sc = new Scanner(System.in); int[] ages = new int[3]; int choice; boolean is2Enabled=false, do{ System.out.println("
在至少选择一次
1
之前,您无法显示选项2
Scanner sc = new Scanner(System.in);
int[] ages = new int[3];
int choice;
boolean is2Enabled=false,
do{
System.out.println("Average Age program");
System.out.println("Enter 1 to enter ages");
if(is2Enabled)
System.out.println("Enter 2 to calculate the average Age");
System.out.println("Enter 0 to Exit");
choice=sc.nextInt();
switch(choice)
{
case 1:
acceptAges(ages);
is2Enabled=true;
break;
case 2:
if(is2Enabled)
averageAge(ages);
else
System.out.print("invalid option");
break;
default:
System.out.print("invalid option");
}
}while(choice != 0);
}
在至少选择一次
1
之前,您无法显示选项2
Scanner sc = new Scanner(System.in);
int[] ages = new int[3];
int choice;
boolean is2Enabled=false,
do{
System.out.println("Average Age program");
System.out.println("Enter 1 to enter ages");
if(is2Enabled)
System.out.println("Enter 2 to calculate the average Age");
System.out.println("Enter 0 to Exit");
choice=sc.nextInt();
switch(choice)
{
case 1:
acceptAges(ages);
is2Enabled=true;
break;
case 2:
if(is2Enabled)
averageAge(ages);
else
System.out.print("invalid option");
break;
default:
System.out.print("invalid option");
}
}while(choice != 0);
}
这是可能的,但我认为最好保持逻辑流这是可能的,但我认为最好保持逻辑流