这是我一直在努力学习Java的简单的酒吧式测验的一些代码。出于某种原因,它会在第一时间问这个问题,然后从那时起就跳过它。它也声明了字符串null,即使我初始化它们。
这是代码
public class PubQuizWMethod
{
private static Scanner keyboard = new Scanner (System.in);
static String a1,a2,a3,a4;
static String b1,b2,b3,b4;
static String c1,c2,c3,c4;
static String d1,d2,d3,d4;
static String ans1,ans2,ans3,ans4;
static String q1,q2,q3,q4;
static String question;
static boolean newQuiz = true;
static boolean notStop = true;
static char correctAnswer;
static char answer1,answer2,answer3,answer4;
static char answer;
static int score = 0;
public static void mainMenu()
{
{
{
do{
gettingQuestion();
question = q1;
gettingAnswers();
ans1 = a1;
ans2 = b1;
ans3 = c1;
ans4 = d1;
gettingCorrectAnswer();
answer1 = correctAnswer;
gettingQuestion();
question = q2;
gettingAnswers();
ans1 = a2;
ans2 = b2;
ans3 = c2;
ans4 = d2;
gettingCorrectAnswer();
answer2 = correctAnswer;
gettingQuestion();
question = q3;
gettingAnswers();
ans1 = a3;
ans2 = b3;
ans3 = c3;
ans4 = d3;
gettingCorrectAnswer();
answer3 = correctAnswer;
gettingQuestion();
question = q4;
ans1 = a4;
ans2 = b4;
ans3 = c4;
ans4 = d4;
gettingCorrectAnswer();
answer4 = correctAnswer;
if(notStop == true)
{
score = 0;
System.out.println(q1);
System.out.println("a: " +a1);
System.out.println("b: " +b1);
System.out.println("c: " +c1);
System.out.println("d: " +d1);
answer = keyboard.next().charAt(0);
if(answer == answer1)
{
System.out.println("That was correct");
score ++;
}
else
{
System.out.println("That was incorrect");
}
System.out.println(q2);
System.out.println("a: " +a2);
System.out.println("b: " +b2);
System.out.println("c: " +c2);
System.out.println("d: " +d2);
answer = keyboard.next().charAt(0);
if(answer == answer2)
{
System.out.println("That was correct");
score ++;
}
else
{
System.out.println("That was incorrect");
}
System.out.println(q3);
System.out.println("a: " +a3);
System.out.println("b: " +b3);
System.out.println("c: " +c3);
System.out.println("d: " +d3);
answer = keyboard.next().charAt(0);
if(answer == answer3)
{
System.out.println("That was correct");
score ++;
}
else
{
System.out.println("That was incorrect");
}
System.out.println(q4);
System.out.println("a: " +a4);
System.out.println("b: " +b4);
System.out.println("c: " +c4);
System.out.println("d: " +d4);
answer = keyboard.next().charAt(0);
if(answer == answer4)
{
System.out.println("That was correct");
score ++;
}
else
{
System.out.println("That was incorrect");
}
System.out.println("You achieved a score of " +score +" out of 4");
System.out.println("Would you like to play again? y/n ");
char yn = keyboard.next().charAt(0);
if(yn == 'y')
{
notStop = true;
}
else
{
notStop = false;
}
}
System.out.println("Would you like to make a new quiz? y/n");
char yn = keyboard.next().charAt(0);
if(yn == 'y')
{
newQuiz = true;
}
else
{
newQuiz = false;
}
}while(newQuiz = true);
}
}
}
public static void gettingQuestion()
{
System.out.println("Please enter the question");
question = keyboard.nextLine();
}//getting the questions
public static void gettingAnswers()
{
System.out.println("Please enter in the four answers each on its own line.");
ans1 = keyboard.nextLine();
ans2 = keyboard.nextLine();
ans3 = keyboard.nextLine();
ans4 = keyboard.nextLine();
}
public static void gettingCorrectAnswer()
{
System.out.println("Please enter the correct answer. a/b/c/d");
correctAnswer = keyboard.next().charAt(0);
}
public static void main(String[] args)
{
mainMenu();
}
}
结果如下:
Please enter the question ahsdf Please enter in the four answers each on its own line. adsfh adsfh asdfh asdfh Please enter the correct answer. a/b/c/d a Please enter the question Please enter in the four answers each on its own line. asdf asdfh asdfh asdfh Please enter the correct answer. a/b/c/d a Please enter the question Please enter in the four answers each on its own line. asdfh asdfh asdfh adsfh Please enter the correct answer. a/b/c/d asdfh Please enter the question Please enter the correct answer. a/b/c/d asdfh null a: null b: null c: null d: null a That was correct null a: null b: null c: null d: null a That was correct null a: null b: null c: null d: null a That was correct null a: null b: null c: null d: null a That was correct You achieved a score of 4 out of 4 Would you like to play again? y/n y Would you like to make a new quiz? y/n n Please enter the question Please enter in the four answers each on its own line.
等
答案 0 :(得分:2)
经典错误:使用==
来比较Strings
而不是equals
。这不是一个bug;这是你和你的代码。
答案 1 :(得分:2)
我已粘贴了一些代码来解释发生了什么。我们从这里开始:
public static void mainMenu()
{
{
{
do{
gettingQuestion();
question = q1;
在下面的一行中,您从键盘读取并加载 ans1,ans2,ans3和ans4 变量中的所有值。
gettingAnswers();
一切都很好,但是当你做到以下几点时:
ans1 = a1;
ans2 = b1;
ans3 = c1;
ans4 = d1;
你覆盖 ans1,ans2,ans3和ans4值,这就是你有 null 值的原因。
作为额外注释,您可以处理数组或对象以保持代码更多顺序,它可能是这样的
public class Question{
private String realAnswer
private String question;
private String[] fakeAnswers = new String[4];
public Question(String realAnswer, String question, String [] fakeAnswers){
this.realAnswer = realAnswer;
this.question = question;
this.fakeAnswers = fakeAnswers;
}
/*
Some getters and setters
*/
}
然后,您可以创建一个包含以下内容的 main 类:
public static void main(String[] args){
// Number 10 is the quantity of questions that your quiz will have
Question[] question = new Question[10];
/*
More code
*/
}
答案 2 :(得分:1)
您在这里为null
分配了question
。
gettingQuestion();
question = q1; // q1 is null.
//这里的问题和q1都是null,因此它在答案中打印为空。
因此输出打印为空。
同时更改while condition
。
答案 3 :(得分:1)
next()
不会处理该行的结尾。因此,当您再次致电nextLine()
时,您将输入之前输入的输入(\n
)作为输入。所以它是“跳过”实际输入,并从\n
错过的上一个输入中吞下next()
。你有两个解决方案:
nextLine()
之前拨打另一个nextLine()
(因此它会吞下\n
)。next
并将其替换为nextLine()
。另一件事:
作业的表达式会返回已分配的值,请看:
while(newQuiz = true);
while
循环的总是 true
。
另请注意,写if(variable == true)
是多余的,只需写if(variable)
即可。