これは、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.
等