有没有办法在满足条件后停止扫描仪输入循环?

Is there a way to stop scanner input loop after the condition has been met?

我试图只允许用户从给定的输入中进行选择。如果他们为第一个 optionGate 选择 "f"、"F"、"c" 或 "C" 以外的东西,那么他们将被提示重试并返回扫描仪。下一个门也是一样的情况,optionGate2.

门通过阻止用户输入与给定选项不同的输入来完成我想要的。当两个布尔值都变成 true 并且门通过时,我的问题就来了。由于某种原因,扫描器仍然在无限循环中要求用户输入。然后我可以一遍又一遍地向扫描仪输入任何内容,程序不再运行。

对正在发生的事情有什么建议或意见吗?

Scanner in = new Scanner(System.in);
boolean optionGate = false;
boolean optionGate2 = false;

String tempLabel = "";    //initialize to F
String precipLabel = ""; //initialize to in

while(optionGate != true){
    System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
    String scaleT = in.nextLine();

    if("f".equalsIgnoreCase(scaleT)){
        tempLabel = "F";
        optionGate = true;
    }
    else if("c".equalsIgnoreCase(scaleT)){
        tempLabel = "C";
        optionGate = true;
    }
    else{
        System.out.println("Error: not an option. Try again:");
        optionGate = false;
    }
}

while(optionGate2 != true){
    System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)");
    String scaleP = in.nextLine();

    if("i".equalsIgnoreCase(scaleP)){
        precipLabel = "in";
        optionGate2 = true;
    }
    else if("c".equalsIgnoreCase(scaleP)){
        precipLabel = "cm";
        optionGate2 = true;
    }
    else{
        System.out.println("Error: not an option. Try again:");
        optionGate2 = false;
    }
}

编辑: 添加上述代码段之后的代码。 (很抱歉明显缺乏 Whosebug 诀窍。这是我在该平台上的第一个 post)。

String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
        double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values

        double[] updatedTemperature;
        updatedTemperature = new double[temperature.length];

        double[] updatedPrecipitation;
        updatedPrecipitation = new double[precipitation.length];


        while("f".equalsIgnoreCase(tempLabel)){
            for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = temperature[i];
        }

             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
       }

        while("c".equalsIgnoreCase(tempLabel)){
           for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
        }


             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }   

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
        }




        //Processing - calculate average temperature and total precipitation
        for( int index = 0; index < updatedTemperature.length; index++)
        {
            sum += updatedTemperature[index];
        }
        avg = sum/updatedTemperature.length;

编辑:这是我的主要方法中的所有代码:


        //Declare and initialize variables
        Scanner in = new Scanner(System.in);
        String city = "Orlando";
        String state = "FL";
        double sum = 0;
        double avg = 0;
        boolean optionGate = false;
        boolean optionGate2 = false;

        String tempLabel = "";    //initialize to F
        String precipLabel = ""; //initialize to in

        while(optionGate != true){
        System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
        String scaleT = in.nextLine();

        if("f".equalsIgnoreCase(scaleT)){
            tempLabel = "F";
            optionGate = true;
        }
        else if("c".equalsIgnoreCase(scaleT)){
            tempLabel = "C";
            optionGate = true;
        }
        else{
            System.out.println("Error: not an option. Try again:");
            optionGate = false;
        }
        }

        while(optionGate2 != true){
        System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)"); 
        String scaleP = in.nextLine();

        if("i".equalsIgnoreCase(scaleP)){
            precipLabel = "in";
            optionGate2 = true;
        }
        else if("c".equalsIgnoreCase(scaleP)){
            precipLabel = "cm";
            optionGate2 = true;
            in.close();
        }
        else{
            System.out.println("Error: not an option. Try again:");
            optionGate2 = false;
        }
        }




        String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
        double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values

        double[] updatedTemperature;
        updatedTemperature = new double[temperature.length];

        double[] updatedPrecipitation;
        updatedPrecipitation = new double[precipitation.length];


        while("f".equalsIgnoreCase(tempLabel)){
            for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = temperature[i];
        }

             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
       }

        while("c".equalsIgnoreCase(tempLabel)){
           for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
        }


             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }   

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
        }




        //Processing - calculate average temperature and total precipitation
        for( int index = 0; index < updatedTemperature.length; index++)
        {
            sum += updatedTemperature[index];
        }
        avg = sum/updatedTemperature.length;

        //Output: display table of weather data including average and total
        System.out.println();
        System.out.println("           Weather Data");
        System.out.println("      Location: " + city +", " + state);
        System.out.println("Month     Temperature (" +  tempLabel + ")     Precipitation (" + precipLabel + ")");
        System.out.println();
        System.out.println("***************************************************");
        for( int index = 0; index < temperature.length; index++)
        {
            System.out.println(month[index] + "\t   " + updatedTemperature[index] + "\t\t       " + updatedPrecipitation[index]);
        }
        System.out.println("***************************************************");
        System.out.println("Average: " + avg + "    Total: " + sum);

您的代码在

行进入循环
while("c".equalsIgnoreCase(tempLabel)){

while("f".equalsIgnoreCase(tempLabel)){

永不退出。

您可以在此处更改 if 构造上的 while