Browse Source

Update QuizCommand.java

Random question picked from json file and printed out with answeres
remotes/origin/quiz
Friederike von Gruben 2 years ago
parent
commit
803ed2f6fc
  1. 23
      src/main/java/org/bitbiome/commands/QuizCommand.java

23
src/main/java/org/bitbiome/commands/QuizCommand.java

@ -1,12 +1,35 @@
package org.bitbiome.commands; package org.bitbiome.commands;
import org.bitbiome.classes.JsonParser;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
public class QuizCommand implements CommandAPI { public class QuizCommand implements CommandAPI {
@Override @Override
public void performCommand(Scanner scanner, boolean isRunning, String message) { public void performCommand(Scanner scanner, boolean isRunning, String message) {
String path = "src\\main\\resources\\quiz.json";
JSONObject quiz = JsonParser.readJSONFile(path);
JSONArray fragen = quiz.getJSONArray("Quiz");
int index = new Random().nextInt(fragen.length());
JSONObject frage = fragen.getJSONObject(index);
JSONArray antworten = frage.getJSONArray("antworten");
System.out.println("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n");
String frageString = frage.getString("frage");
System.out.println(frageString);
for (int i = 0; i < antworten.length(); i++) {
String antwort = antworten.getString(i);
System.out.println(i + 1 + ". " + antwort);
}
} }
} }

Loading…
Cancel
Save