由于无法在循环中再次获得用户输入,程序重复了开关情况

时间:2019-02-01 03:57:03

标签: java loops user-input do-loops

我几乎完成了一个Java类项目的入门课程,在该课程中,用户必须对汽油值进行转换。当我通过命令提示符放置该程序时,它运行良好,直到选择转换的某一点为止(从选项1、2、3、4或5)。

发生的事情是该程序对其进行了精确计算,并显示“如果要退出则键入NO”部分就很好了,但是随后在用户有机会键入任何内容之前重复执行该程序。我不确定在这里做什么。我尝试键入keyboard.nextLine();,因此它将跳到用户可以发送输入的行,但是发生了相同的错误。如果在yesOrNo = yes时更改底部的行以运行循环,那么它将突然停止程序。我认为这是同样的问题,尽管我不确定该怎么做,而且在其他论坛帖子中也找不到此问题。感谢任何决定帮助的人。

这是我的代码,减去主要方法/类标题:

    // Ask the user for their name
    System.out.println("What is your name?");

    // Use Scanner to get the user's name (a string variable)
    String name = keyboard.nextLine();

    // Welcome the user by name & give a readable description of the program using escape sequences
    System.out.println(name + ", welcome. I am Rachel and this program will help you perform gasoline analysis.\n");
    System.out.println("Using this program, you can extract information about any amount of gallons of gasoline, such as:");
    System.out.println("its equivalent in liters, its price, and how much oil is required to produce it, for example.\n");
    System.out.println("Press any letter to continue.");

    String yesOrNo = keyboard.next();
    boolean performConversion = true;


    // Perform the conversions the user wants at least once, until they want to stop. 
    do 
{
        // Get the value of gallons (floating-point value) of gasoline from the user.
            System.out.println("Please enter a number that will represent your gallons of gasoline.");
            double userGallons = keyboard.nextDouble();

        // Declare variables and convert the gallons to the other units of measurement. 
            double liters = (userGallons * 3.7854);

            double barrelsNeeded = (userGallons / 19.5);

            double poundsCO2 = (userGallons * 20.0);

            double ethanolEnergy = (userGallons * 75700);

            double price = (userGallons * 4.0);

        // Show the user a menu of their choices for conversion.
            System.out.println("Select the conversion you would like to perform here:");
            System.out.println("Press 1 for liters");
            System.out.println("2 for pounds of CO2 produced");
            System.out.println("3 for equivalent energy amount of ethanol gallons");
            System.out.println("4 for price in USD");
            System.out.println("or 5 for barrels of oil required to produce that amount of gasoline.");
            int userChoice = keyboard.nextInt();

        // Display the original gallons and the available conversions to the user. 

        switch(userChoice)
        {
            case 1:
                System.out.println("Original gallons of gasoline: " + userGallons + "gallons");
                System.out.println("Amount in liters: " + liters);
                System.out.println("Type NO if you want to exit, or any other key to continue.");
                break;
            case 2:
                performConversion = true;
                System.out.println("Original gallons of gasoline: " + userGallons + "gallons");
                System.out.println("Pounds of CO2 produced : " + poundsCO2);
                System.out.println("Type NO if you want to exit, or any other key to continue.");
                break;
            case 3:
                performConversion = true;
                System.out.println("Original gallons of gasoline: " + userGallons + "gallons");
                System.out.println("Equivalent energy amount in ethanol gallons: " + ethanolEnergy + " BTU");
                System.out.println("Type NO if you want to exit, or any other key to continue.");
                break;
            case 4:
                performConversion = true;
                System.out.println("Original gallons of gasoline: " + userGallons + "gallons");
                System.out.println("Price in USD: $" + price);
                System.out.println("Type NO if you want to exit, or any other key to continue.");
                break;
            case 5:
                performConversion = true;
                System.out.println("Original gallons of gasoline: " + userGallons + "gallons");
                System.out.println("Number of barrels of oil needed to produce that amount of gallons: " + barrelsNeeded);
                System.out.println("Type NO if you want to exit, or any other key to continue.");
                break;
            default:
                System.out.println("Invalid character. Please enter 1, 2, 3, 4, or 5.");
        }
    } while (!"no".equalsIgnoreCase(yesOrNo));
    System.out.println("Goodbye! The program will now end.");
}

}

2 个答案:

答案 0 :(得分:0)

无需进行任何重组,就可以由此更改while条件:

} while (!"no".equalsIgnoreCase(keyboard.next()) );

答案 1 :(得分:0)

由于您希望转换至少发生一次,因此建议您将这两个语句(如下所述)移至do { //... } while();循环的最后(即,在switch...case语句之后) :

System.out.println("Press any letter to continue.");

String yesOrNo = keyboard.next();

原因

您要实现的是让用户执行转换,然后让他们选择是否继续。

当前程序不要求用户进行此选择。相反,它只是循环使用yesOrNo变量的值,该变量是在进入循环之前设置的

在循环的最后保留上述语句应该会为您提供所需的结果。

此外,我看到您已经创建了一个变量performConversion,但是它从未在某个地方使用(至少在提供的代码段中没有使用)。因此,除非在main()方法的其他位置使用此变量,否则建议您删除它。