を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
このコードを実行するとエラーが発生していますか? –
私は少なくとも今のところランタイムやコンパイラのエラーは出ません。質問を読み込んだときにすべての選択肢が表示されないだけです。それは私が正解を置くと私にエラーを与える。本質的に論理エラーです。 – ABY
サンプルの 'quiz.txt'ファイルを含めることができますか? – apicellaj