我花了两个小时试图找出为什么布尔表达式总是返回从一开始就分配的任何值,而不是从正确的条件语句中获取值。在这种情况下,boolean question从questionOneAnswer()方法返回true,即使我的输入A或C或者D,这意味着分数加到即使答案是错的。但是,如果我分配boolean question给 false inquestionOneAnswer()并且我的输入是B(这是正确答案),则不会执行score()方法中的代码qOneCheck,因此,分数保持为 0。
import java.util.Scanner;
import java.util.Random;
class miniProject
{
public static void main(String[] args)
{
questionOne();
questionOneAnswer();
int scr = score();
System.out.println("Your score is " + scr);
System.exit(0);
}
/* *********************************
This method asks for a question and gives 4 possible answers
*/
public static void questionOne()
{
System.out.println("Please, type the letter which you think
contain the right answer!");
System.out.println(" ");
System.out.println("How many oscars did the Titanic movie
got?");
System.out.println("A. 12 B.11 C.3 D.32");
return; // Ends the method
}
public static int score()
{
boolean qOneCheck = questionOneAnswer();
int currentScore = 0;
int oldScore = 0;
int newScore = 0;
int random = randomGen();
if(qOneCheck == true)
{
currentScore = currentScore + random;
newScore = currentScore;
}
else
{
currentScore = oldScore;
}
return newScore;
}
编辑:删除后questioOneAnswer()我终于得到了结果。感谢你们所有人。我现在终于要睡觉了哈哈。
沧海一幻觉
相关分类