java 使用 while 循环返回开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5739200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:32:36  来源:igfitidea点击:

Using a while loop to go back to start

java

提问by Tuan Nguyen

/--------------------------------------- -----------------Quizzes.java------------ ----------------------------------------/ import java.util.Scanner; import java.text.NumberFormat;

/ --------------------------------------- ---------- -------Quizzes.java------------ ---------------------------- ------------/ 导入 java.util.Scanner; 导入 java.text.NumberFormat;

public class Quizzes { public static void main(String[] args) {

公共类测验{ public static void main(String[] args) {

     Scanner scan = new Scanner(System.in);
        NumberFormat per = NumberFormat.getPercentInstance();     


  //User to input keys for quiz
     System.out.println("How many questions are in the quiz?");
     int numques = scan.nextInt();
     int[] keys = new int[numques];

     for (int i=0; i<keys.length; i++)
     {
        System.out.print("Please enter the key for # " + (i+1)+ ": " );
        keys[i] = scan.nextInt();         
     }

  //User to input answer
     System.out.println("\nGrading Quizzes");
     System.out.println("--------------------");

        int correct=0;

     for (int i=0; i<keys.length; i++)
     {
        System.out.println("Please enter the answer for # " + (i+1)+ ":  ");
        int answer= scan.nextInt();
            if (answer== keys [i])
            {
                System.out.print("Number "+(i+1)+" is correct.\n");
                correct++;
            }
            else 
            {
                System.out.print("Number "+(i+1)+" is incorrect.\n");

            }
     }
    double cal=(double) correct/numques;
        System.out.println("Total number of correct is "+correct);
     System.out.println("The percent correct is " +per.format(cal));

        System.out.println("Would you like to grade another quiz? (y/n)");
        String user_input=scan.next();

        while(user_input.equals("y"))
        {   correct=0;
     for (int i=0; i<keys.length; i++)
     {
        System.out.println("Please enter the answer for # " + (i+1)+ ":  ");
        int answer= scan.nextInt();
            if (answer== keys [i])
            {
                System.out.print("Number "+(i+1)+" is correct.\n");
                correct++;
            }
            else {
            System.out.print("Number "+(i+1)+" is incorrect.\n");   }

            } 
           cal=(double) correct/numques;
           System.out.println("Total number of correct is "+correct);
        System.out.println("The percent correct is " +per.format(cal));
      }
           System.out.println("Goodbye!");

 }

}

}

How would I make the program go back to where the user would have to enter the answer using the while loop? No matter what I tried, right when it prints out, "Would you like to grade another quiz, and if the user type y, it would just end. Can anyone point out what I'm doing wrong

我如何让程序回到用户必须使用 while 循环输入答案的地方?无论我尝试什么,当它打印出来时,“你想给另一个测验评分吗,如果用户输入 y,它就会结束。谁能指出我做错了什么

Edit 1: Well I got it to re-run again after the while loop but it keep asking me to input the answer for the question over and over and over again, it doesn't break out of the loop,it doesn't go back to the part where it asked if I wanted to grade another. This is the output

编辑 1:嗯,我让它在 while 循环后再次重新运行,但它一直要求我一遍又一遍地输入问题的答案,它不会跳出循环,它不会去回到它询问我是否想给另一个评分的部分。这是输出

How many questions are in the quiz? 2 Please enter the key for # 1: 1 Please enter the key for # 2: 2

测验中有多少个问题?2 请输入#1:1的密钥 请输入#2:2的密钥

Grading Quizzes

评分测验

Please enter the answer for # 1:
1 Number 1 is correct. Please enter the answer for # 2:
3 Number 2 is incorrect. Total number of correct is 1 The percent correct is 50% Would you like to grade another quiz? (y/n) y Please enter the answer for # 1:
1 Number 1 is correct. Please enter the answer for # 2:
2 Number 2 is correct. Total number of correct is 2 The percent correct is 100% Please enter the answer for # 1:
1 Number 1 is correct. Please enter the answer for # 2:
2 Number 2 is correct. Total number of correct is 2 The percent correct is 100% Please enter the answer for # 1:

请输入 #1 的答案:
1 数字 1 是正确的。请输入 #2 的答案:
3 数字 2 不正确。正确总数为 1 正确百分比为 50% 您想为另一个测验评分吗?(y/n) y 请输入#1 的答案:
1 数字 1 是正确的。请输入 #2 的答案:
2 数字 2 是正确的。正确总数为 2 正确百分比为 100% 请输入#1 的答案:
1 数字 1 是正确的。请输入 #2 的答案:
2 数字 2 是正确的。正确总数为 2 正确百分比为 100% 请输入#1 的答案:

采纳答案by Prince John Wesley

while(user_input.equals('y'))

should be

应该

while(user_input.equals("y")) // see the double quotes here

Problem with your code is that character 'y' is autoboxed to instance of Characterclass which is the subclass of Object.

您的代码的问题是字符 'y' 被自动装箱到Character类的实例,它是Object的子类。

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class

类对象是类层次结构的根。每个类都有一个 Object 作为超类。所有对象,包括数组,都实现了这个类的方法

So is the candidate for public boolean equals(Object).

的候选人也是如此public boolean equals(Object)

Stringimplementation of boolean equals(Object)checks whether the instance is of type String. if it fails, it will simply return false. If you want to compare with 'y', then try this.

String的实现boolean equals(Object)检查实例是否为类型String。如果失败,它将简单地返回false。如果你想与 比较'y',那么试试这个。

while(user_input.equals(((Character)'y').toString()))

回答by Mukesh Singh Rathaur

You will replace this while with your while(user_input.equals("y"))loop and the problem you pointed will go away but i think still your code will not work as you expected. I think your will need these more correction on your code

你将用你的while(user_input.equals("y"))循环替换它,你指出的问题将会消失,但我认为你的代码仍然不会像你预期的那样工作。我认为您需要对代码进行更多更正

  1. while loop should be this one while(user_input.equalsIgnoreCase("y"))
  2. Put the opening and closing braces for while loop and in end while after finishing your for loop put these two statement to check again and again.
  3. follow the following snap of code

    while(user_input.equalsIgnoreCase("y")) {

        for (int i=0; i<keys.length; i++)
        {
            ....               
        }
    
        System.out.println("Would you like to grade another quiz? (y/n)");
        user_input=scan.next();
    }
    
  1. while 循环应该是这个 while(user_input.equalsIgnoreCase("y"))
  2. 将左括号和右括号放在 while 循环中,并在结束 for 循环后将这两个语句一次又一次地检查。
  3. 遵循以下代码快照

    while(user_input.equalsIgnoreCase("y")) {

        for (int i=0; i<keys.length; i++)
        {
            ....               
        }
    
        System.out.println("Would you like to grade another quiz? (y/n)");
        user_input=scan.next();
    }
    

回答by allthenutsandbolts

I will put the question a while loop and it will be something like

我会把这个问题放在一个while循环中,它会是这样的

while (true) {

 // ask the users the questions 

 // ask if he wants to continue... if no then break; else continue

}

}

回答by Dair

Ok add a while(true)at the beginning of your program and add the appropriate brackets to encompass the whole method's contents. Then, delete the current while loop you have. Say after you get your input for user_input:

好的while(true),在程序的开头添加 a并添加适当的括号以包含整个方法的内容。然后,删除当前的 while 循环。在获得 user_input 的输入后说:

if (user_input.equals("y")) {
}
else {
    break;
}