满足条件后是否有停止扫描仪输入回路的方法?

问题描述 投票:-2回答:2

我试图仅允许用户从给定的输入中进行选择。如果他们为第一个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;
    }
}

EDIT:添加上述片段之后的代码。 (很抱歉,明显缺少stackoverflow专有技术。这是我在平台上的第一篇文章。)>

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;

EDIT:

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

        //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);

我试图仅允许用户从给定的输入中进行选择。如果他们为第一个optionGate选择了“ f”,“ F”,“ c”或“ C”以外的其他东西,则会提示他们再试一次,然后...

java loops boolean infinite
2个回答
0
投票

您的代码在行中输入循环


-3
投票

欢迎使用Stackverflow!也许您需要的是在使用它后将其扫描仪close()

© www.soinside.com 2019 - 2024. All rights reserved.