Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制用户在选项2之前选择选项1_Java - Fatal编程技术网

Java 强制用户在选项2之前选择选项1

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。任何帮助都将不胜感激。 (注意,到目前为止,我只在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("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);
}

这是可能的,但我认为最好保持逻辑流这是可能的,但我认为最好保持逻辑流