Java中的2D数组?

时间:2012-05-04 20:07:07

标签: java arrays

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

在测试者中,将每个玩家的已回答问题的信息添加到2d阵列中(2个玩家,每个5个问题)。

显示正确答案(在问题中调用新方法),然后显示带有玩家答案的2D数组。最后,显示每个玩家和赢得的玩家的积分。“

有关我如何做到这一点的任何帮助?我感谢你能给我的任何帮助。

到目前为止,这是我的代码,首先是我的班级名为“问题”,其次是名为“Trivia Game”的测试人员

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;
  }
}

/////////// TESTER

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! ");
  }
}

1 个答案:

答案 0 :(得分:2)

在java中,你可以声明数组数组,然后它们是二维的:

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

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

for (String[] questionAnswers : allAnswers) {

变量questionAnswers是一个String数组。然后,您可以遍历此内部数组,并设置答案:

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

请注意,java中的数组始终从零开始,因此第一个答案属于索引0。

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

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

如果您不太清楚,请阅读java中的a tutorial on multidimensional arrays