So bin ich ein Quiz-Programm zu schreiben, die eine ungewisse Anzahl der Fragen in es enthält (in Java) und ich habe Probleme, die folgenden Dinge erreichen:Probleme aus der Datei laden zu Arraylist
1) Laden Quizfrage aus der Datei und in Arraylist speichern und den Zugriff auf sie (die Hilfe benötigen!) 2) richtige Antwort nicht akzeptiert - gibt mir Fehler (logische Fehler) 3) nicht alle Antwortmöglichkeiten angezeigt werden
Hier ist der Code:
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();
}
}
}
und die Frage Klasse:
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
Erhalten Sie irgendwelche Fehler, wenn dieser Code ausgeführt wird? –
Ich bekomme keine Runtime oder Compiler Fehler für jetzt. Es zeigt nur nicht alle Optionen an, wenn ich die Frage lade. es gibt mir auch Fehler, wenn ich die richtige Antwort gebe. also im Wesentlichen ein logischer Fehler. – ABY
Können Sie eine Beispieldatei 'quiz.txt' hinzufügen? – apicellaj