【发布时间】:2015-11-10 01:48:08
【问题描述】:
代码的主体应该一直运行到用户输入了至少 10 个问题,输入的问题不超过 20 个,并且直到学生的 >正确百分比为 85% 或更高。
这不是全部,但我认为只有 while 循环最麻烦。我已经尝试了多种组合以使代码按上述方式运行,包括我给出的方式。我已经测试了所有变量,并且它们确实在程序运行时保持跟踪,但是我怎样才能让这段代码像规范所说的那样运行?
到目前为止,我有这个:
//Declaring int variables to store values of correct/wrong problems
int numCorrect = 0;
int numWrong = 0;
int totalQuestions = 0;
double percent = 0;
int subNumCorrect = 0;
int subNumWrong = 0;
int addNumCorrect = 0;
int addNumWrong = 0;
//Storing the users answers as integers, after asking them for a solution to a random problem
//Then looping this code 20 times
while((totalQuestions <= 5 && percent >= 85.0 || totalQuestions < 10)) {
Question theQuestion = new Question();
Scanner scanner = new Scanner(System.in);
System.out.println("What is the result?" + "\n" + theQuestion.toString());
int result = scanner.nextInt();
// Checks if the question is addition/subtraction and correct/wrong, then increases the variables 1
if((theQuestion.getOperator() == '+') && (result == theQuestion.determineAnswer()))
addNumCorrect += 1;
else if(theQuestion.getOperator() == '+' && (result != theQuestion.determineAnswer()))
addNumWrong += 1;
if((theQuestion.getOperator() == '-') && (result == theQuestion.determineAnswer()))
subNumCorrect += 1;
else if((theQuestion.getOperator() == '-') && (result != theQuestion.determineAnswer()))
subNumWrong += 1;
//Determines if the users answer is correct or not, and displays a message accordingly
//As well as keeping count of how many correct/wrong answers are attempted by the user and storing them
//in the variable numCorrect/numWrong
if(result == theQuestion.determineAnswer()) {
numCorrect += 1;
System.out.println("Congratulations, you got it correct!");
}
else if(result != theQuestion.determineAnswer()) {
numWrong += 1;
System.out.println("The correct answer for " + theQuestion.toString()
+ " is " + theQuestion.determineAnswer());
}
//Increasing value of totalQuestions by one every loop
totalQuestions++;
//Multiplying the number of correct problems by 100 and dividing the product by the number of
//total questions. Then rounding the quotient to get the percentage of correct answers
percent = Math.round((double)(numCorrect * 100) / (totalQuestions));
}
//Progress report displaying how many addition and subtraction problems the user got correct and wrong
//Then determines the percentage of correct problems the user entered
System.out.println("\nProgress Report:" + "\nAddition:\nYou got " + addNumCorrect
+ " correct and " + addNumWrong + " incorrect.");
System.out.println("Subtraction:\nYou got " + subNumCorrect
+ " correct and " + subNumWrong + " incorrect.");
System.out.println("Number of problems: " + totalQuestions);
System.out.println("The percent correct: " + percent + "%");
}
}
【问题讨论】:
-
那么到底是什么问题,循环停止了吗?
-
在这种情况下,我使用 10 和 5 而不是 20 和 10,因为它使我在测试时更容易和更快。所以当我运行它时,它应该停止时继续运行(如果正确百分比高于 85% 并且用户输入了至少 5 个问题,它应该停止)。对于在 while 循环中使用什么条件来获得问题开头所述的条件,我感到非常困惑。
-
@JawadLeWywadi 我在哪里失踪()?
-
如果他达到 20 个问题但结果低于 85 会怎样?我的意思是你需要它做什么。
-
问题来自于你的 85% 不清楚如果你有超过 20 个问题而你没有 85% 会发生什么......
标签: java eclipse while-loop coding-style do-while