2016-03-26 3 views
0

をArrayListにそして、のArrayListに格納し、それへのアクセス(ヘルプが必要!) 2)正しい答えは受け入れられません - すべての回答の選択肢がここ 問題は、だから私は(Javaで)それに質問の不確か#が含まれているクイズ番組を書いていますし、私は次のことを達成する問題を抱えています

に表示されていない(論理エラー) 3)私にエラーを与えるのコード:

import java.io.*; 
import java.util.*; 
import java.text.*; 
import java.math.*; 

public class Quiz { 

public void writeFile() { 
    Question qn = new Question(); 
    try { 
    PrintWriter out = new PrintWriter("quiz.txt"); 
    out.println(qn.Question); 
    out.println(qn.numberOfChoices); 
    qn.answerChoices = new String[qn.numberOfChoices]; 
    for (int i = 0; i < qn.numberOfChoices; i++) { 
     out.println(qn.answerChoices[i]); 
    } 
    out.println(qn.correctAnswer); 
    out.println(qn.numOfTries); 
    out.println(qn.numOfCorrectTries); 
    out.close(); 
    } catch (IOException f) { 
    System.out.println("Error."); 
    } 
    qn.getQuestion(); 
} 

public void readFile() { 
    File file = new File ("quiz.txt"); 
    boolean exists = file.exists(); 
    Quiz q = new Quiz(); 
    Question a = new Question(); 
    List<String> question = new ArrayList<String>(); 
    String[] answerChoices = a.answerChoices; 
    try { 
     if (exists == true) { 
      Scanner s = new Scanner(file); 
      a.Question = s.nextLine(); 
      a.numberOfChoices = s.nextInt(); 
      a.answerChoices = new String[a.numberOfChoices]; 
      for (int i = 0; i < a.numberOfChoices; i++) { 
       a.answerChoices[i] = s.nextLine(); 
      } 
      s.nextLine(); 
      a.correctAnswer = s.nextInt(); 
      a.numOfTries = s.nextInt(); 
      a.numOfCorrectTries = s.nextInt(); 
      a.getQuestion(); 
     } else { 
      q.writeFile(); 
     } 
    } catch (IOException e) { 
     System.out.println("File not found."); 
    } 
} 

public static void main (String[] args) { 
    Scanner in = new Scanner(System.in); 
    Quiz qz = new Quiz(); 
    Question b = new Question(); 
    int Selection; 
    List<String> question = new ArrayList<String>(); 

    System.out.println("Welcome to the Quiz Program! Good luck!"); 
    do { 
     qz.readFile(); 
     System.out.println("Your answer?: "); 
     Selection = in.nextInt(); 
     if (Selection == b.correctAnswer) { 
      b.numOfCorrectTries++; 
      b.getQuestion(); 
     } else { 
      b.getQuestion(); 
     } 
    } while (Selection < b.numberOfChoices); 
    while (Selection > b.numberOfChoices || Selection < 0) { 
     System.out.println("Error. Try again"); 
     System.out.println("Your answer?: "); 
     Selection = in.nextInt(); 
    } 
    } 
} 

そして、質問クラス:

import java.io.*; 
import java.util.*; 
import java.text.*; 
import java.math.*; 

public class Question { 

int correctAnswer; 
int numOfTries; 
int numOfCorrectTries; 
int numberOfChoices; 
String Question; 
String[] answerChoices; 

public Question() { 

} 
public void getQuestion() { 
    System.out.println("Question: " + Question); 
    System.out.println("Answer: "); 
    for (int i = 0; i < numberOfChoices; i++) { 
     System.out.println(answerChoices[i]); 
    } 
} 

public double getPercentageRight() { 
    double percentageRight = (numOfCorrectTries/numOfTries) * 100; 
    percentageRight = Math.round(percentageRight * 100); 
    percentageRight = percentageRight/100; 
    return percentageRight; 
} 
} 

QUIZ.TXT:

How many licks does it take to get to the tootsie roll center of a 
tootsie pop? 
4 
one 
two 
three 
four 
2 
14 
5 
What is your name? 
3 
Arthur, King of the Britons 
Sir Lancelot the Brave 
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot 
0 
14 
6 
Who's on first? 
5 
What 
Why 
Because 
Who 
I don't know 
3 
14 
7 
Which of the following is a terror of the fire swamp? 
4 
Lightning sand 
Flame spurt 
R.O.U.S. 
All of the above 
3 
14 
4 
Who is the all-time greatest pilot? 
6 
Manfred von Richthofen 
Chuck Yeager 
Hiraku Sulu 
Luke Skywalker 
Kara Thrace 
Charles Lindbergh 
4 
14 
9 
+0

このコードを実行するとエラーが発生していますか? –

+0

私は少なくとも今のところランタイムやコンパイラのエラーは出ません。質問を読み込んだときにすべての選択肢が表示されないだけです。それは私が正解を置くと私にエラーを与える。本質的に論理エラーです。 – ABY

+0

サンプルの 'quiz.txt'ファイルを含めることができますか? – apicellaj

答えて

1

いくつかの問題:

あなたList<String> question = new ArrayList<String>();ではなく、文字列(代わりの質問オブジェクトとして、すべてを保持するため、List<Question> questionBank = new ArrayList<Question>();のようなものでなければなりません)はかなり厄介である。 questionBankという名前は、コードを読むときにquestionよりも説明的です。私はまたquestionBankをクラス変数としてお勧めしますので、Quizクラスを通して簡単にアクセスできます。

あなたはあなたのArrayListに質問を追加することはありませんが、あなたはすでにそれを知っていると思っています。

Questionクラスもちょっと変わっています。それはanswerChoices.lengthと同じなので、私はnumberOfChoicesための変数を削除

public class Question { 

    private int correctAnswer; 
    private int numOfTries; 
    private int numOfCorrectTries; 
    private String question; 
    private String[] answerChoices; 

    public Question(String question, String[] answerChoices, 
      int correctAnswer, int numOfTries, int numOfCorrectTries) { 
     this.question = question; 
     this.answerChoices = answerChoices; 
     this.correctAnswer = correctAnswer; 
     this.numOfTries = numOfTries; 
     this.numOfCorrectTries = numOfCorrectTries; 
    } 

    public void getQuestion() { 
      System.out.println("Question: " + question); 
      System.out.println("Answer: "); 
      for (int i = 0; i < answerChoices.length; i++) { 
       System.out.println(answerChoices[i]); 
      } 
    } 

    public double getPercentageRight() { 
     double percentageRight = (numOfCorrectTries/numOfTries) * 100; 
     percentageRight = Math.round(percentageRight * 100); 
     percentageRight = percentageRight/100; 
     return percentageRight; 
    } 

} 

:それを構造化するためのより良い方法は、次のようなものかもしれません。 Questionの名前をquestionに変更しました。Javaの変数は通常camelCaseに従うためです。私は他の方法が何であるか、どのように出力を表示すべきかについてはわかりません。

ファイルに読み込む場合は、あなたが持っているものと似たようなことができると思いますが、Questionクラスの新しいコンストラクタに準拠するコードを投稿します。

private void addQuestions() { 
    File quizText = new File("quiz.txt"); 
    try { 
     Scanner fileIn = new Scanner(quizText); 
     while (fileIn.hasNextLine()) { 
      String question = fileIn.nextLine(); 
      int numberOfAnswers = fileIn.nextInt(); 
      fileIn.nextLine(); 
      String[] answers = new String[numberOfAnswers]; 
      for (int i = 0; i < numberOfAnswers; i++) { 
       answers[i] = fileIn.nextLine(); 
      } 
      int correctAnswer = fileIn.nextInt(); 
      int numOfTries = fileIn.nextInt(); 
      int numOfCorrectTries = fileIn.nextInt(); 
      fileIn.nextLine(); 
      Question nextQuestion = 
       new Question(question, answers, correctAnswer, numOfTries, numOfCorrectTries); 
      questionBank.add(nextQuestion); 
     } 
     fileIn.close(); 
    } catch (IOException e){ 
     e.printStackTrace(); 
     System.out.println("File Not Found."); 
     return; 
    } 
} 

私はまた、変数は非公開とが、それらは、外部から直接アクセス(および/または変更)されないようにするために、カスタムゲッターを作成することができます。このコードを使用して、5つの質問すべてを含む質問銀行を作成し、すべての可能な選択肢とともに正しい答えを表示できるようになりましたので、うまくいけば正しい方向にあなたを指しています。

+0

私はList questionBank = new List questionBankを使用していますが、引数の不一致がありますが、どうすればこの問題を解決できますか?また、ファイルからの質問とその回答の選択肢などを読み込みますが、arrayListに格納する方法やarrayListからアクセスする方法がわかりません。私はこれをうまくやり遂げる方法を知りました – ABY

+0

ArrayListが必要なので、このリストのように初期化する必要があります。リスト questionBank = new ArrayList <>(); 'クラス変数にすることを強くお勧めします。 'addQuestions()'メソッドです。上記のメソッドを使うと 'Question'オブジェクトが' ArrayList'の中に格納されます。 'Question'クラスの情報にアクセスするには、そうするためにそのクラスの中にメソッドを作成する必要があります。 – apicellaj

+0

私はしましたが、質問クラスの代わりにクイズクラスに入れたいと思ったらどうしますか? – ABY

0

私はあなたのクイズクラスを再テストしました。私の例では、上記のすべての質問に答えます。私はそれらを一つずつ説明します。

public class Quiz { 

List<Question> question = new ArrayList<Question>(); 

public void writeFile() { 
    Question qn = new Question(); 
    try { 
     PrintWriter out = new PrintWriter("quiz.txt"); 
     out.println(qn.Question); 
     out.println(qn.numberOfChoices); 
     qn.answerChoices = new String[qn.numberOfChoices]; 
     for (int i = 0; i < qn.numberOfChoices; i++) { 
      out.println(qn.answerChoices[i]); 
     } 
     out.println(qn.correctAnswer); 
     out.println(qn.numOfTries); 
     out.println(qn.numOfCorrectTries); 
     out.close(); 
    } catch (IOException f) { 
     System.out.println("Error."); 
    } 
    qn.getQuestion(); 
} 

public void readFile() { 
    File file = new File("quiz.txt"); 
    boolean exists = file.exists(); 
    Quiz q = new Quiz(); 
    Question a = new Question(); 
    String[] answerChoices; 
    try { 
     if (exists == true) { 
      Scanner s = new Scanner(file); 
      String line = ""; 
      while (s.hasNextLine()) { 
       line = s.nextLine(); 
       if (line.startsWith("---")) { 
        a = new Question(); 
        a.Question = line.substring(4); 
       } else if (line.startsWith("choices : ")) { 
        a.numberOfChoices = Integer.parseInt(line.substring(10).trim()); 
        a.answerChoices = new String[a.numberOfChoices]; 
        for (int i = 0; i < a.numberOfChoices; i++) { 
         a.answerChoices[i] = s.nextLine(); 
        } 
       } else if (line.startsWith("correct answer : ")) { 
        a.correctAnswer = Integer.parseInt(line.substring(17).trim()); 

       } else if (line.startsWith("No of Tries : ")) { 
        a.numOfTries = Integer.parseInt(line.substring(14).trim()); 

       } else if (line.startsWith("No of correct Tries : ")) { 
        a.numOfCorrectTries = Integer.parseInt(line.substring(22).trim()); 
        question.add(a); 
       } 
      } 
     } else { 
      q.writeFile(); 
     } 
    } catch (IOException e) { 
     System.out.println("File not found."); 
    } 
} 

public static void main(String[] args) { 
    Quiz qz = new Quiz(); 
    qz.readFile(); 
    Question b = new Question(); 
    int selection; 

    //real program starts here 
    System.out.println("Welcome to the Quiz Program! Good luck!"); 
    System.out.println("****************************************"); 

    b = qz.question.get(2); // you can implement how your questions are taken from list of questions 
    b.getQuestion(); 
    Scanner in = new Scanner(System.in); 
    System.out.print("Your answer?: "); 
    selection = in.nextInt(); 

    if (selection == b.correctAnswer) { 
     b.numOfCorrectTries++; 
     System.out.println("Correct answer"); 

    } else { 
     System.out.println("Incorrect answer"); 
    } 
} 

} 

1)ロードクイズの質問ファイルからと(助けが必要ArrayListの中に保存し、それをアクセス!)の代わりにクラス変数として以下

List<String> question = new ArrayList<String>(); 

使用を用いる

。つまり、あなたのクラス

List<Question> question = new ArrayList<Question>(); 

2)受け入れられない正解内の任意の場所にアクセスすることができます - 私はエラー(論理エラー)を与える

ロジックは、メインブロックで修正されています。

3)は、すべての解答選択肢が

再なかったあなたreadFileの()メソッドが表示されません。このロジックで成功するには、以下のようにquiz.txtを再作成する必要があります。

--- How many licks does it take to get to the tootsie roll center of a tootsie pop? 
choices : 4 
one 
two 
three 
four 
correct answer : 2 
No of Tries : 14 
No of correct Tries : 5 
--- What is your name? 
choices : 3 
Arthur, King of the Britons 
Sir Lancelot the Brave 
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot 
correct answer : 0 
No of Tries : 14 
No of correct Tries : 6 
--- Who's on first? 
choices : 5 
What 
Why 
Because 
Who 
I don't know 
correct answer : 3 
No of Tries : 14 
No of correct Tries : 7 
--- Which of the following is a terror of the fire swamp? 
choices : 4 
Lightning sand 
Flame spurt 
R.O.U.S. 
All of the above 
correct answer : 3 
No of Tries : 14 
No of correct Tries : 4 
--- Who is the all-time greatest pilot? 
choices : 6 
Manfred von Richthofen 
Chuck Yeager 
Hiraku Sulu 
Luke Skywalker 
Kara Thrace 
Charles Lindbergh 
correct answer : 4 
No of Tries : 14 
No of correct Tries : 9 

は注:質問の 選択はメインで

b = qz.question.get(2); 

()に基づいています。無作為に選ばれた質問に基づいて質問用紙を作成するには、別の方法を作成し、この方法の質問をすることがあります。さらにMath.random()を参照してください。

この種のアプリケーションでは、txtの代わりにXMLファイルを作成する方がよい。 Googleの "Java and XML"

希望します。

関連する問題