Java クラスの課題で、2 人のプレーヤー間で Tria Game を作成するプログラムを作成することになっています。プログラムは完了していますが、彼女は私たちにも追加してほしいと言っています。説明どおりに Question クラスを完成させます。10 個の回答の配列を 2D 配列に変換し、2D 配列を返すメソッドを追加します。
テスターで、各プレーヤーの回答済みの質問の情報を 2 次元配列 (2 人のプレーヤー、それぞれ 5 つの質問) に追加します。
正解を表示し (Question で新しいメソッドを呼び出します)、2D 配列をプレイヤーの回答と共に表示します。最後に、各プレイヤーと勝ったプレイヤーのポイントを表示します。」
これを行うにはどうすればよいですか?あなたが私に与えることができる助けに感謝します。
これまでのコードは次のとおりです。最初は「質問」と呼ばれる私のクラスで、2番目は「トリビアゲーム」と呼ばれるテスターです
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! ");
}
}