gpt4 book ai didi

java - 使用 while 循环返回开始

转载 作者:行者123 更新时间:2023-11-30 09:49:30 24 4
gpt4 key购买 nike

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

公开课测验 { 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!");

}

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

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

测验有多少道题?2个请输入#1 的 key :1请输入#2 的 key :2

评分测验

请输入#1的答案:
1个1号是正确的。请输入#2 的答案:
3个数字 2 不正确。正确总数为1正确率是 50%你想给另一个测验评分吗? (是/否)是请输入#1 的答案:
1个1号是正确的。请输入#2 的答案:
2个2号是正确的。正确总数为2正确率是 100%请输入#1 的答案:
1个1号是正确的。请输入#2 的答案:
2个2号是正确的。正确总数为2正确率是 100%请输入#1 的答案:

最佳答案

while(user_input.equals('y'))

应该是

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

您的代码存在问题,字符“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

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

boolean equals(Object)

String 实现检查实例是否为String 类型。如果失败,它将简单地返回 false。如果您想与 'y' 进行比较,请试试这个。

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

关于java - 使用 while 循环返回开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5739200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com