Java 执行while循环时不打印正确的变量
我正在做一个编程任务,它会问用户一些问题,如果用户得到了错误/正确的答案,那没关系,使用随机数生成器来确定他们对该问题的分数。然而,如果他们选择“不可能的答案”(不可能的答案),他们的分数将重置为0 当我运行代码时 如果我在第一个问题上得到23分,在第二个问题上得到0分,那么我在第二个问题上的分数应该重置为0分,但最后总分显示为23分 任何帮助都将不胜感激Java 执行while循环时不打印正确的变量,java,arrays,if-statement,Java,Arrays,If Statement,我正在做一个编程任务,它会问用户一些问题,如果用户得到了错误/正确的答案,那没关系,使用随机数生成器来确定他们对该问题的分数。然而,如果他们选择“不可能的答案”(不可能的答案),他们的分数将重置为0 当我运行代码时 如果我在第一个问题上得到23分,在第二个问题上得到0分,那么我在第二个问题上的分数应该重置为0分,但最后总分显示为23分 任何帮助都将不胜感激 import java.util.Scanner; public class Main { public static doubl
import java.util.Scanner;
public class Main {
public static double randnum(){
int random = (int)(Math.random() * 50 + 1);
return random;
}
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int score1 = 0;
int score2 = 0;
int score3 = 0;
int totalscore = 0;
final double NumberofQuestions = 2;
String[][] questions ={
{"What is the largest bone in the human body? ", "\n Choose 1 for Femur \n Choose 2 for Tibia \n Choose 3 for Palatine Bone \n Choose 4 for Tongue ", "1"},
{"What is the capital of Albania? ! ","\n Choose 1 for Shkoder \n Choose 2 for Tirana \n Choose 3 for Durres \n Choose 4 for Rome ", "2"}
};
String[] Answers = new String[(int) NumberofQuestions];
int x=0;
do
{
System.out.print("" + (x+1) + ". " + questions[x][0] + " "+questions[x][1]);
Answers[x] = String.valueOf(input.nextInt());
Answers[x].toLowerCase();
if(questions[x][2].equals(Answers[x])) {
score1 = (int) randnum();
System.out.println("Correct: " + score1 + " points");
totalscore = totalscore + score1;
}
if (Answers[x].equals("4")){
System.out.println("\n Thats a impossible answer! The right answer is "+questions[x][2]);
totalscore = 0;
score2 = 0;
System.out.println(score2 + " points");
} else {
System.out.println("\nIncorrect. The right answer is "+questions[x][2]);
score3 = (int) randnum();
System.out.println(score3 + " points");
totalscore = totalscore + score3;
}
System.out.print("\n");
x++;
} while(x < NumberofQuestions); //close outer loop
totalscore = score1 + score2 + score3;
System.out.println("\n\t\tYou got " + totalscore + " points !\n\n\n");
System.exit(0);
}
}
import java.util.Scanner;
公共班机{
公共静态双随机数(){
int random=(int)(Math.random()*50+1);
返回随机;
}
静态扫描仪输入=新扫描仪(System.in);
公共静态void main(字符串[]args){
int评分1=0;
int评分2=0;
int评分3=0;
整数总分=0;
最终双数问题=2;
字符串[][]问题={
{“人体最大的骨骼是什么?”,“\n股骨选择1\n胫骨选择2\n腭骨选择3\n舌头选择4”,“1”},
{“阿尔巴尼亚的首都是什么?!”,“\n选择1代表什科德\n选择2代表地拉那\n选择3代表杜勒斯\n选择4代表罗马”,“2”}
};
字符串[]答案=新字符串[(int)NumberofQuestions];
int x=0;
做
{
系统输出打印(“+(x+1)+”+问题[x][0]+“+问题[x][1]);
Answers[x]=String.valueOf(input.nextInt());
答案[x].toLowerCase();
如果(问题[x][2]。等于(答案[x])){
score1=(int)randnum();
System.out.println(“正确:“+score1+”分”);
总分=总分+总分1;
}
如果(回答[x]。等于(“4”)){
System.out.println(“\n这是一个不可能的答案!正确答案是“+问题[x][2]);
总分=0;
得分2=0;
System.out.println(分数2+“分”);
}否则{
System.out.println(“\n不正确。正确答案是”+问题[x][2]);
score3=(int)randnum();
System.out.println(分数3+“分”);
总分=总分+总分3;
}
系统输出打印(“\n”);
x++;
}while(x
解决当前代码问题的快速方法:
while(x<NumberofQuestions)
{ …
还要注意,当你只问2个问题时,你使用了3个分数变量解决当前代码问题的快速方法:
while(x<NumberofQuestions)
{ …
还要注意,当你只问2个问题时,你使用了3个分数变量这行代码正在撤消您在循环过程中进行的所有
totalscore
计算:
totalscore = score1 + score2 + score3;
因为它看起来像<代码> SCORE1,<代码> SCORE2,和
totalscore = score1 + score2 + score3;
因为它看起来像<代码> SCORE1,<代码> SCORE2,和
while
循环之后,您设置了totalscore=score1+score2+score3;
,这将覆盖它之前的任何值。”。“如果我在第一个问题上得了23分,在第二个问题上得了0分,那么第二个问题上我的分数应该会重置为0,但最后总分显示为23分。“您希望哪一行代码为您执行此操作?在第二个if语句中,我将totalscore设置为0。这是否可行?在while
循环之后,您设置了totalscore=score1+score2+score3;
,这将覆盖它之前的任何值。”。