由于在循环期间无法再次获取用户输入而对切换情况进行程序重复



我几乎完成了一个介绍类的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.");
}

}

在不进行任何重组的情况下,您可以更改您的 while 条件:

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

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

System.out.println("Press any letter to continue.");
String yesOrNo = keyboard.next();

原因

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

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

将上述语句保持在循环的最末尾应该会给您带来所需的结果。

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

相关内容

  • 没有找到相关文章

最新更新