Java TriviaGame方法打印错误的值

时间:2018-02-08 23:01:36

标签: java

我为两名球员创造了一个琐事游戏。它每个询问他们5个问题,并在每个问题之后显示答案是否正确或不正确,如果不正确则显示正确答案。程序编译并运行并正确显示,直到显示正确答案,无论实际答案是什么,它总是显示3。

任何帮助将不胜感激!

import java.util.*;
import java.util.ArrayList;

public class Question
{
private ArrayList <String> questions;
private int answerNumber;
private int questionNumber;
private int [] answers = {3,2,4,3,3,4,4,1,3,3};

public Question()
{
}
public String getQuestion(int number)
{
    return createQuestions(number);
}
public String toString ()
{
    return "Question for player+";
}
public String createQuestions(int questionNumber)
{
    ArrayList<String> questions = new ArrayList<>();

    String question1 = "Who's proffesion was a chef?";
    String question2 = "Who had a son named Ben?";
    String question3 = "Who claimed they were on a break?";
    String question4 = "Who's pick up line was \"How you doin\"?";
    String question5 = "What animal is Chandler afraid of?";
    String question6 = "Who married a gay ice dancer?";
    String question7 = "Who did Rachel almost marry?";
    String question8 = "Who had a pet monkey?";
    String question9 = "Where did the show take place?";
    String question10 = "What was the name of the coffee house?";

    questions.add (question1);
    questions.add (question2);
    questions.add (question3);
    questions.add (question4);
    questions.add (question5);
    questions.add (question6);
    questions.add (question7);
    questions.add (question8);
    questions.add (question9);
    questions.add (question10);

    return questions.get(questionNumber);
}
public String getPossibleAnswers(int questionNumber)
{
    ArrayList <String> possibleAnswers = new ArrayList<>();

    String possibleAnswers1 = "1. Ross\n2. Rachel\n3. Monica\n4. Ross and Monica's mom";
    String possibleAnswers2 = "1. Chandler\n2. Ross\n3. Rachel\n4. Phoebe";
    String possibleAnswers3 = "1. Rachel\n2. Monica\n3. Joey\n4. Ross";
    String possibleAnswers4 = "1. Joey\n2. Ross\n3. Chandler\n4. Phoebe";
    String possibleAnswers5 = "1. Cats\n2. Snakes\n3. Dogs\n4. Monkeys";
    String possibleAnswers6 = "1. Phoebe\n2. Rachel\n3. Janice\n4. Monica";
    String possibleAnswers7 = "1. Ross\n2. Chandler\n3. Gunther\n4. Barry";
    String possibleAnswers8 = "1. Chandler\n2. Ross\n3. Joey\n4. Phoebe";
    String possibleAnswers9 = "1. Atlanta\n2. Detroit\n3. New York City\n4. Las Vegas";
    String possibleAnswers10 = "1. Central Coffee\n2. Coffee Central\n3. Central Perk\n4. There was no coffee shop";

    possibleAnswers.add (possibleAnswers1);
    possibleAnswers.add (possibleAnswers2);
    possibleAnswers.add (possibleAnswers3);
    possibleAnswers.add (possibleAnswers4);
    possibleAnswers.add (possibleAnswers5);
    possibleAnswers.add (possibleAnswers6);
    possibleAnswers.add (possibleAnswers7);
    possibleAnswers.add (possibleAnswers8);
    possibleAnswers.add (possibleAnswers9);
    possibleAnswers.add (possibleAnswers10);

    if (questionNumber == 0)
        answerNumber = 0;
    else if (questionNumber == 1)
        answerNumber = 1;
    else if (questionNumber ==2)
        answerNumber = 2;
    else if (questionNumber == 3)
        answerNumber = 3;
    else if (questionNumber == 4)
        answerNumber = 4;
    else if (questionNumber == 5)
        answerNumber = 5;
    else if (questionNumber == 6)
        answerNumber = 6;
    else if (questionNumber == 7)
        answerNumber = 7;
    else if (questionNumber == 8)
        answerNumber = 8;
    else answerNumber = 9;

    return possibleAnswers.get(answerNumber);
}
public int getCorrectAnswer(int questionNumberAsked)
{
    int answer;

    if (questionNumberAsked == 1)
        answer = answers[0];
    else if (questionNumber == 2)
        answer = answers[1];
    else if (questionNumberAsked == 3)
        answer = answers[2];
    else if (questionNumberAsked == 4)
        answer = answers[3];
    else if (questionNumberAsked == 5)
        answer = answers[4];
    else if (questionNumberAsked == 6)
        answer = answers[5];
    else if (questionNumberAsked == 7)
        answer = answers[6];
    else if (questionNumberAsked == 8)
        answer = answers[7];
    else if (questionNumberAsked == 9)
        answer = answers[8];
    else answer = answers[9];

    return answer;
}

public int getRandomQuestionNumber()
{
    Random random = new Random();
    int questionNumber = random.nextInt(answers.length);

    return questionNumber;
}
public int displayQuestion(int playerNum)
{
        int questionNumber = getRandomQuestionNumber();

        // Display the player number.
        System.out.println("Question for player #" + playerNum);
        System.out.println("------------------------");

        // Display the question.
        System.out.println(getQuestion(questionNumber));
        System.out.println(getPossibleAnswers(questionNumber));

        return questionNumber;
}
}

import java.util.Scanner;

public class TriviaGame
{
public static void main (String [] args)
{
    Scanner keyboard = new Scanner (System.in);
    Question question = new Question ();
    int userAnswer = 0;
    int player1Points = 0;
    int player2Points = 0;

    for (int i = 0; i < 5; i++)
    {
        int questionNumber = question.displayQuestion(1);
        int player1Answer = keyboard.nextInt();

        if (player1Answer == question.getCorrectAnswer(questionNumber))
        {
            System.out.println("That is correct!");
            player1Points ++;
        }
        else System.out.println ("Sorry the correct answer was "+question.getCorrectAnswer(questionNumber));

        int questionNumberPlayer2 = question.displayQuestion(2);
        int player2Answer = keyboard.nextInt();

        if (player2Answer == question.getCorrectAnswer(questionNumberPlayer2))
        {
            System.out.println("That is correct!");
            player2Points ++;
        }
        else System.out.println ("Sorry the correct answer was "+question.getCorrectAnswer(questionNumberPlayer2));

    }

    System.out.println ("Game Over!\n----------------\nPlayer 1 points: "+player1Points+"\nPlayer 2 points "+
    player2Points);

    if (player1Points > player2Points)
        System.out.println ("Player 1 wins!");
    else if (player2Points < player1Points)
        System.out.println ("Player 2 wins!");
    else System.out.println ("It's a tie!");

}
}

1 个答案:

答案 0 :(得分:0)

您的getCorrectAnswer()方法根本不知道被询问的随机问题是什么!并且在同一个方法中,你有最后一个阻止设置为:

else answer = answers[9];

由于类级别的int questionNumber的默认值为0且与任何条件不匹配,因此根据最后一个else answers[9]数组,answer的值始终为3。

  

使其有效:

将你的getCorrectAnswer()方法更改为,以便它知道questionNumber问的是什么..

    public int getCorrectAnswer(int questionNumberAsked) {
    int answer;

    if (questionNumberAsked == 0)
        answer = answers[0];
    else if (questionNumberAsked == 1)
        answer = answers[1];
    else if (questionNumberAsked == 2)
        answer = answers[2];
    else if (questionNumberAsked == 3)
        answer = answers[3];
    else if (questionNumberAsked == 4)
        answer = answers[4];
    else if (questionNumberAsked == 5)
        answer = answers[5];
    else if (questionNumberAsked == 6)
        answer = answers[6];
    else if (questionNumberAsked == 7)
        answer = answers[7];
    else if (questionNumberAsked == 8)
        answer = answers[8];
    else if (questionNumberAsked == 9)
        answer = answers[9];
    else answer = 0;

    return answer;
}

并在同一个类make displayQuestion方法返回生成的randomQuestionNumber,并被问为:

public int displayQuestion(int playerNum)
{
        int questionNumber = getRandomQuestionNumber();
        // Display the player number.
        System.out.println("Question for player #" + playerNum);
        System.out.println("------------------------");

        // Display the question.
        System.out.println(getQuestion(questionNumber));
        System.out.println(getPossibleAnswers(questionNumber));
        return questionNumber;
}

最后在main方法中将那个questionNumber传递给:

int questionNumber = question.displayQuestion(1);
        int player1Answer = keyboard.nextInt();

        if (player1Answer == question.getCorrectAnswer(questionNumber))
        {
            System.out.println("That is correct!");
            player1Points ++;
        }
        else System.out.println ("Sorry the correct answer was "+question.getCorrectAnswer(questionNumber));

        int questionNumberForPlayer2 =  question.displayQuestion(2);
        int player2Answer = keyboard.nextInt();

        if (player2Answer == question.getCorrectAnswer(questionNumberForPlayer2))
        {
            System.out.println("That is correct!");
            player2Points ++;
        }
        else System.out.println ("Sorry the correct answer was "+question.getCorrectAnswer(questionNumberForPlayer2));

    }