-3

对于我的 Java 课程中的一项作业,我应该编写一个程序,用于在两个玩家之间创建一个 Tria 游戏,我已经完成了该程序,但她希望我们也可以添加它。“按照描述完成 Question 类。添加一个方法,将 10 个答案的数组转换为 2D 数组并返回 2D 数组。

在测试器中,将每个玩家回答的问题的信息添加到一个二维数组中(2 个玩家,每个 5 个问题)。

显示正确答案(调用问题中的新方法),然后显示包含玩家答案的二维数组。最后,显示每个玩家和获胜玩家的分数。”

对我如何做这件事有任何帮助吗?我很感激你能给我的任何帮助。

到目前为止,这是我的代码,首先是我的类名为“问题”,第二个是测试者名为“琐事游戏”

public class Question
{
  String question;
  String possibleAns1;
  String possibleAns2;
  String possibleAns3;
  String possibleAns4;
  int CorrectAnsNum;

  Question(String ques, String ans1, String ans2, String ans3, String ans4, int num)
  {
    question = ques;
    possibleAns1 = ans1;
    possibleAns2 = ans2;
    possibleAns3 = ans3;
    possibleAns4 = ans4;
    CorrectAnsNum = num;
  }

  Question(){
  }

  String getQuestion(){
    return question;
  }

  String getAns1(){
    return possibleAns1;
  }

  String getAns2(){
    return possibleAns2;
  }

  String getAns3(){
    return possibleAns3;
  }

  String getAns4(){
    return possibleAns4;
  }

  int getCorrectAnsNum(){
    return CorrectAnsNum;
  }

  void setQuestion(String newQuestion){
    question = newQuestion;
  }

  void setAns1(String newPossibleAns1){
    possibleAns1 = newPossibleAns1;
  }

  void setAns2(String newPossibleAns2){
    possibleAns2 = newPossibleAns2;
  }

  void setAns3(String newPossibleAns3){
    possibleAns3 = newPossibleAns3;
  }

  void setAns4(String newPossibleAns4){
    possibleAns4 = newPossibleAns4;
  }

  void setCorrectAnsNum(int newCorrectAnsNum){
    CorrectAnsNum = newCorrectAnsNum;
  }
}

///////////测试仪

import java.util.ArrayList;
import java.util.Scanner;

public class TriviaGame
{
  public static void main(String args[])
  {
    Scanner input = new Scanner(System.in);

    int pointsPlayer1 = 0;
    int pointsPlayer2 = 0;

    Question q1 = new Question("How many days are there in a week?", "5", "2", "7", "57", 3);
    Question q2 = new Question("What is the first month of the year?", "February", "January", "December", "August", 2);
    Question q3 = new Question("How many hours are there in one day?", "32", "12", "0", "24", 4);
    Question q4 = new Question("What is the capital of Massachusetts?", "Framingham", "Worcester", "Boston", "Springfield", 3);
    Question q5 = new Question("At what age are you legally aloud to buy lottery tickets", "18", "16", "21", "19", 1);
    Question q6 = new Question("What sport can you hit a homerun in?", "Golf", "Football", "Soccer", "Baseball", 4);
    Question q7 = new Question("Which one of these fruits is yellow?", "Apple", "Banana", "Strawberry", "Grape", 2);
    Question q8 = new Question("Who is the current president of the United States?", "Bill Clinton", "George Bush", "Barack Obama", "Jason Statham", 3);
    Question q9 = new Question("What is the highway speed limit in most areas?", "85", "65", "35", "110", 2);

    Question q10 = new Question();
    q10.setQuestion("What is the company that makes the iPod?");

    q10.setAns1("Microsoft");
    q10.setAns2("Verizon");
    q10.setAns3("Apple");
    q10.setAns4("Walmart");
    q10.setCorrectAnsNum(3);

    ArrayList<Question> questionList = new ArrayList<Question>();

    questionList.add(q1);
    questionList.add(q2);
    questionList.add(q3);
    questionList.add(q4);
    questionList.add(q5);
    questionList.add(q6);
    questionList.add(q7);
    questionList.add(q8);
    questionList.add(q9);
    questionList.add(q10);

    System.out.println("****Player1****");

    for(int i = 0; i < 5; i++)
    {
      Question q = questionList.get(i);

      System.out.println("\n" + (i+1) + ". " + q.getQuestion());
      System.out.println("1) " + q.getAns1());
      System.out.println("2) " + q.getAns2());
      System.out.println("3) " + q.getAns3());
      System.out.println("4) " + q.getAns4());

      System.out.print("Enter your answer: ");
      int userAns = input.nextInt();

      if (userAns == q.getCorrectAnsNum())
        pointsPlayer1++;
    }

    System.out.println("\n****Player2****");

    for(int i = 5; i < 10; i++)
    {
      Question q = questionList.get(i);

      System.out.println("\n" + (i+1) + ". " + q.getQuestion());
      System.out.println("1) " + q.getAns1());
      System.out.println("2) " + q.getAns2());
      System.out.println("3) " + q.getAns3());
      System.out.println("4) " + q.getAns4());

      System.out.print("Enter your answer: ");
      int userAns = input.nextInt();

      if (userAns == q.getCorrectAnsNum())
        pointsPlayer2++;
    }

    System.out.println("\n****Points Earned****");
    System.out.println("Player 1: " + pointsPlayer1);
    System.out.println("Player 2: " + pointsPlayer2);

    if (pointsPlayer1>pointsPlayer2)
      System.out.println("\nPlayer 1 is the winner! ");
    else if (pointsPlayer1<pointsPlayer2)
      System.out.println("\nPlayer 2 is the winner! ");
    else
      System.out.println("\nIt's a tie! ");
  }
}
4

1 回答 1

2

在 java 中,您可以声明数组数组,它​​们是二维的:

String[][] allAnswers = new String[10][4];

现在,您可以遍历外部数组:

for (String[] questionAnswers : allAnswers) {

该变量questionAnswers是一个字符串数组。然后,您可以遍历这个内部数组,并设置答案:

questionAnswers[0] = "Answer 1";
// ...

注意java中的数组总是从零开始的,所以第一个答案属于索引0。

您还可以使用索引运算符访问特定字段:

System.out.println("Second answer of question 3 is: " questionAnswers[2][1];

如果这对您来说不是很清楚,您应该阅读有关java 中的多维数组的教程。

于 2012-05-04T22:03:02.450 回答