简体   繁体   English

如何在 while 循环中为不同的变量添加 if 语句?

[英]How do you put an if statement for a different variable in a while loop?

I have created a program that calculated gamer xp.我创建了一个计算游戏玩家 xp 的程序。 It uses a while loop to loop asking the user if they want to add another gamer calculation.它使用 while 循环来循环询问用户是否要添加另一个玩家计算。

        System.out.print("\nWould you like to enter a gamer's data? Yes/No => ");
        String choice = scanChoice.nextLine();
  
        //Loop to get multiple gamer inputs
        while (true) {

            if (choice.equals("Yes")) {

                //Prompt user for gamer's name
                System.out.print("Enter gamer's name=> ");
                gamerName = scanName.nextLine();

                //Prompt user for gamer's scores
                System.out.print("Enter gamer's Level XP scores seperated by spaces: L1 L2 L3 ES => ");
                L1 = scanNum.nextDouble();
                L2 = scanNum.nextDouble();
                L3 = scanNum.nextDouble();
                ES = scanNum.nextDouble();

                while (L1 < 20 || L2 < 20 || L3 < 20 || ES <20) {

                    System.out.print("please enter an experience number between 20 and 100 in increments of 5.")
                }
                
                //Calculate score
                double score = L1 + L1 * 0.20 + L2 + L2 * 0.30 + L3 + L3 * 0.50 + ES + ES * 0.60;

                //Output results
                System.out.println("\nGamer Name: " + gamerName + " L1=" + L1 + " L2=" + L2 + " L3=" + L3 + " ES=" + ES);
                System.out.println("Final XP score with bonuses=" + score); 

                System.out.print("\nDo you want to enter another gamer's data? Yes/No => ");
                String choiceLoop = scanChoice.nextLine();

            }
                
            //Breaks loop
            else if (choice.equals("No")) {
            
                //Goodbye
                System.out.println("\nThank you for using Tyler's Total Experience Calculator.");

                break;
            }   
                
            //If incorrect input is entered, restarts loop
            else {

                System.out.print("Wrong Input.");
            }

My issue is here:我的问题在这里:

while (true) {

            if (choice.equals("Yes")) {

                //Prompt user for gamer's name
                System.out.print("Enter gamer's name=> ");
                gamerName = scanName.nextLine();

                //Prompt user for gamer's scores
                System.out.print("Enter gamer's Level XP scores seperated by spaces: L1 L2 L3 ES => ");
                L1 = scanNum.nextDouble();
                L2 = scanNum.nextDouble();
                L3 = scanNum.nextDouble();
                ES = scanNum.nextDouble();

                if (L1 < 20 || L2 < 20 || L3 < 20 || ES <20) {

                    System.out.print("please enter an experience number between 20 and 100 in increments of 5.")
                }
                
                //Calculate score
                double score = L1 + L1 * 0.20 + L2 + L2 * 0.30 + L3 + L3 * 0.50 + ES + ES * 0.60;

                //Output results
                System.out.println("\nGamer Name: " + gamerName + " L1=" + L1 + " L2=" + L2 + " L3=" + L3 + " ES=" + ES);
                System.out.println("Final XP score with bonuses=" + score); 

                System.out.print("\nDo you want to enter another gamer's data? Yes/No => ");
                String choiceLoop = scanChoice.nextLine();

            }

I am attempting to add an if statement inside of the while loop that will check to make sure that the XP values (L1, L2, L3, and ES) are all between 20 and 100 and in increments of 5. When I run the program, it appears to completely skip over:我正在尝试在 while 循环中添加一个 if 语句,该语句将检查以确保 XP 值(L1、L2、L3 和 ES)都在 20 到 100 之间并且以 5 为增量。当我运行程序时,它似乎完全跳过:

 if (L1 < 20 || L2 < 20 || L3 < 20 || ES <20) {

                    System.out.print("please enter an experience number between 20 and 100 in increments of 5.")
                }

and runs the program without checking that L1, L2, L3, and ES are between 20 and 100. Can anyone explain to me why this is and how to fix it?并在不检查 L1、L2、L3 和 ES 是否在 20 到 100 之间的情况下运行程序。谁能向我解释这是为什么以及如何解决它?

Below is the current result:以下是当前结果:

Welcome to Tyler's Total Experience Calculator!欢迎来到泰勒的总经验计算器!

This program will calculate your total experience gain including engagement bonuses.该程序将计算您的总经验增益,包括参与奖金。

Would you like to enter a gamer's data?您想输入游戏玩家的数据吗? Yes/No => Yes是/否 => 是

Enter gamer's name=> John Doe输入玩家姓名=> John Doe

Enter gamer's Level XP scores seperated by spaces: L1 L2 L3 ES => 12 12 12 12输入以空格分隔的玩家等级 XP 分数:L1 L2 L3 ES => 12 12 12 12

Gamer Name: John Doe L1=12.0 L2=12.0 L3=12.0 ES=12.0玩家姓名:John Doe L1=12.0 L2=12.0 L3=12.0 ES=12.0

Final XP score with bonuses=67.2带有奖金的最终 XP 分数 = 67.2

Do you want to enter another gamer's data?您想输入其他玩家的数据吗? Yes/No => No是/否 => 否

Thank you for using Tyler's Total Experience Calculator.感谢您使用 Tyler 的总经验计算器。

A bit embarrassing, the issue was that I created a new file for the code and did not make it a.java file, so when I would run it would run the original version of the file.有点尴尬,问题是我为代码创建了一个新文件,并没有把它变成一个.java 文件,所以当我运行它时,它会运行该文件的原始版本。 I assumed this meant that the things I was writing were being skipped for some reason, but in reality, it was running a different file....我认为这意味着我正在编写的内容由于某种原因被跳过,但实际上,它运行的是不同的文件......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何在if语句中放入if,else语句 - How do you put a if, else statement within a for statement 如何在 while 循环中获取所有用户输入并将它们放入打印语句中 - How do I take all of the user inputs in a while loop and put them into a print statement 如何在Robot Class KeyEvent参数中放置一个String变量? - How do you put a String variable in the Robot Class KeyEvent parameter? JAVA如何将全局变量放入函数参数中? - JAVA How do you put a global variable into a function parameter? 为什么 java 中的 For 循环在 print 语句中放置一个空格然后打印变量会给出不同的结果 - Why For loop in java gives different results if we put a space in the print statement and then print the variable 如何使用try和资源将while循环放入线程? - how do I put a while loop into a thread, using try with resources? 如何通过do while循环将用户输入转换为星号? - How do you convert the users input into asterisks with a do while loop? 在while语句中不接受变量 - Do while statement not accepting variable 如何在Java中使用if-else语句进行do…while循环? - How to make a do…while loop with an if-else statement in java? 如何在 while 循环中只打印一次语句? - How do I only print statement once in a while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM