Java 使用while循环返回开始

Java 使用while循环返回开始,java,Java,/--------------------------------------- -----------------Quizzes.java------------ ----------------------------------------/ 导入java.util.Scanner; 导入java.text.NumberFormat 公开课测验 { 公共静态void main(字符串[]args) { Scanner scan=新的扫描仪(System.in); NumberFormat

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

公开课测验 { 公共静态void main(字符串[]args) {

Scanner scan=新的扫描仪(System.in);
NumberFormat per=NumberFormat.getPercentInstance();
//用户输入测验的键
System.out.println(“测验中有多少问题?”);
int numques=scan.nextInt();
int[]键=新的int[numques];

对于(inti=0;i我将把这个问题放在一个while循环中,它类似于

while (true) {

 // ask the users the questions 

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

确定在程序开始时添加一个
while(true)
,并添加适当的括号以包含整个方法的内容。然后,删除当前的while循环。在获得用户输入的输入后说:

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

while(user_input.equals("y")) // see the double quotes here
代码的问题是字符“y”自动装箱到的是的子类
character
类的实例

类对象是类的根 层次结构。每个类都有一个对象作为 超类。所有对象,包括 数组,实现此 阶级

公共布尔等于(对象)
的候选者也是如此

String
实现
boolean equals(Object)
检查实例是否为
String
类型。如果失败,它将返回
false
。 如果要与
'y'
进行比较,请尝试此操作

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

您将使用
while(user_input.equals(“y”)
循环替换此while,您指出的问题将消失,但我认为您的代码仍无法按预期工作。我认为您需要对代码进行更多更正

  • while循环应该是这个
    while(用户输入.equalsIgnoreCase(“y”))
  • 将while循环的开始大括号和结束大括号放在后面,而在完成for循环后,将这两个语句放在后面反复检查
  • 遵循下面的代码快照

    while(用户输入.equalsIgnoreCase(“y”)) {


    for(int i=0;i用户输入不应该在while循环中吗?程序格式不正确,难以理解。您的方法(
    main
    )太长了,人类无法进行推理。建议您拿出两个内部方法,这将使您更容易对正在查看的控制流进行推理。@allthenutsandbolts提供了一个不错的开始大纲。。。
    while(user_input.equals("y")) // see the double quotes here
    
    while(user_input.equals(((Character)'y').toString()))
    
        for (int i=0; i<keys.length; i++)
        {
            ....               
        }
    
        System.out.println("Would you like to grade another quiz? (y/n)");
        user_input=scan.next();
    }