我正在尝试使用do while语句(在此必需)来声明一个人获得的分数,但是我的do语句中的嵌套if语句似乎没有正确递增(即,无论是否会给出0%什么)?我写错了什么不是递增我的正确变量?
以下是我的代码
Scanner input = new Scanner(System.in);
int q1 = 2, //answer 1 correct answer
q2 = 1, //answer 2 correct answer
q3 = 3, //answer 3 correct answer
correct = 0, //amount correct
answer1, //store answer1
answer2, //store answer2
answer3; //store answer 3
System.out.printf("%n Question #1.) What is the capital of Iowa?"); //display question
System.out.printf("%n 1.) Iowa City \t 2.) Des Moines \t 3.) Dubuque"); //display answer
System.out.printf("%n Your answer: ");
answer1 = input.nextInt(); //input becomes initial value
while (answer1 < 1 || answer1 > 3) //while loop to check if answer is valid
{
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for input if not valid
answer1 = input.nextInt();
}
switch (answer1) //switch statement for input types of answer1
{
case 1:
System.out.printf("%n Incorrect"); //if input 1 it is incorrect
break; //go to next part of code
case 2:
System.out.printf("%n Correct"); //if input 2 it is correct
break;
case 3:
System.out.printf("%n Incorrect"); //if input 3 it is incorrect
break;
}
System.out.printf("%n Question #2.) Where is Iowa?"); //display question
System.out.printf("%n 1.) Midwest \t 2.) South \t 3.) Northeast"); //display answer
System.out.printf("%n Your answer: ");
answer2 = input.nextInt(); //input becomes initial value
while (answer2 < 1 || answer2 > 3) //make sure answer is valid
{
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for valid input
answer2 = input.nextInt();
}
switch (answer2) //switch statement for chosen answers
{
case 1:
System.out.printf("%n Correct"); //correct if input is 1
break;
case 2:
System.out.printf("%n Incorrect"); //incorrect for answer 2
break;
case 3:
System.out.printf("%n Incorrect"); //incorrect for answer 3
break;
}
System.out.printf("%n Question #3.) Who is the new president of UIowa?"); //display question
System.out.printf("%n 1.) Sally Mason \t 2.) Barack Obama \t 3.) Bruce Harreld"); //display answer
System.out.printf("%n Your answer: ");
answer3 = input.nextInt(); //input becomes initial value
while (answer3 < 1 || answer3 > 3) //make sure valid answer
{
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for valid answer
answer3 = input.nextInt();
}
switch (answer3) //switch to give output depending on answer
{
case 1:
System.out.printf("%n Incorrect"); //if input is 1
break;
case 2:
System.out.printf("%n Incorrect"); //if input is 2 this statement
break;
case 3:
System.out.printf("%n Correct"); //if input is 3 this statement
break;
}
do {
if (answer1 == q1) {
correct++;
}
if (answer2 == q2) {
correct++;
}
if (answer3 == q3) {
correct++;
}
} while (correct <= 3);
if (correct == 3) {
System.out.printf("%n 100%%");
} else if (correct == 2) {
System.out.printf("%n 66.67%%");
} else if (correct == 1) {
System.out.printf("%n 33.33%%");
} else {
System.out.printf("%n 0%%");
}
} //end method
} //end class
答案 0 :(得分:1)
主要问题是你实际上并不需要do-while循环 - 删除它并显示正确的结果。
在您继续循环直到正确的变量值为4或更大时,会发生什么。 (这意味着如果没有正确回答你的问题,你就会有一个无限循环。)当你的逻辑去打印结果时,正确的值为4或更多意味着没有触发if条件,导致最终否则执行并显示0%。
还有许多其他方法可以简化和改进您的代码 - 我假设您只是在学习该语言。