Ένα από τα οφέλη της εκμάθησης προγραμματισμού σε cross-platform γλώσσα όπως η Java είναι ότι αν χρειάζεστε ένα εξειδικευμένο εργαλείο, όπως ένα πρόγραμμα quiz που επιλέγει τυχαία ερωτήσεις από μια τράπεζα, μπορείτε να το φτιάξετε μόνοι σας. Είναι επίσης χρήσιμο project για αρχάριους, διότι τους εισάγει στο random utility της γλώσσας και δίνει άσκηση με control structures όπως loops.
import java.util.*;
public class QuizGame {
private static class Question {
String text;
String[] options; // A, B, C, D
char correctAnswer;
Question(String text, String[] options, char correctAnswer) {
this.text = text;
this.options = options;
this.correctAnswer = Character.toUpperCase(correctAnswer);
}
void display() {
System.out.println(text);
for (int i = 0; i < options.length; i++) {
System.out.println((char)('A' + i) + ": " + options[i]);
}
}
boolean checkAnswer(char answer) {
return Character.toUpperCase(answer) == correctAnswer;
}
}
public static void main(String[] args) {
// Τράπεζα ερωτήσεων
List questionBank = Arrays.asList(
new Question("Ποια είναι η πρωτεύουσα της Γαλλίας;",
new String[]{"Λονδίνο", "Παρίσι", "Βερολίνο", "Μαδρίτη"}, 'B'),
new Question("Ποιος γράφει το '1984';",
new String[]{"Αλντους Χάξλι", "Τζορτζ Οργουэл", "Ρέι Μπράντμπερι", "Φίλιπ Κ. Ντικ"}, 'B'),
new Question("Ποιο είναι το μεγαλύτερο πλανήτη του ηλιακού συστήματος;",
new String[]{"Σαтурνας", "Ιούπιτερ", "Ουρανός", "Νέπτιουν"}, 'B'),
new Question("Σε ποια γλώσσα γράφεται η Java;",
new String[]{"C", "C++", "Java", "Python"}, 'C'),
new Question("Τι είναι το 2^10;",
new String[]{"512", "1024", "2048", "1000"}, 'B')
);
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("=== QUIZ GAME ===");
System.out.print("Πόσες ερωτήσεις θέλετε; (1-" + questionBank.size() + "): ");
int numQuestions = scanner.nextInt();
scanner.nextLine(); // consume newline
numQuestions = Math.max(1, Math.min(numQuestions, questionBank.size()));
int score = 0;
Set usedIndices = new HashSet<>();
for (int round = 1; round <= numQuestions; round++) {
// Επιλογή τυχαίας ερώτησης που δεν έχει χρησιμοποιηθεί
int index;
do {
index = random.nextInt(questionBank.size());
} while (usedIndices.contains(index));
usedIndices.add(index);
Question q = questionBank.get(index);
System.out.println("\n--- Ερώτηση " + round + "/" + numQuestions + " ---");
q.display();
System.out.print("Η απάντησή σας (A/B/C/D): ");
String input = scanner.nextLine().trim().toUpperCase();
char answer = input.isEmpty() ? ' ' : input.charAt(0);
if (q.checkAnswer(answer)) {
System.out.println("✅ Σωστά!");
score++;
} else {
System.out.println("❌ Λάθος! Η σωστή απάντηση ήταν: " + q.correctAnswer);
}
}
System.out.println("\n=== ΤΕΛΙΚΟ ΑΠΟΤΕΛΕΣΜΑ ===");
System.out.println("Σκορ: " + score + "/" + numQuestions);
double percentage = (double) score / numQuestions * 100;
System.out.printf("Ποσοστό: %.1f%%%n", percentage);
if (percentage == 100) System.out.println("🏆 τέλειο!");
else if (percentage >= 70) System.out.println("👏 Πολύ καλά!");
else if (percentage >= 50) System.out.println("👍 Καλή προσπάθεια!");
else System.out.println("📚 Ξαναπροσπαθήστε!");
scanner.close();
}
}
Set για αποφυγή διπλών ερωτήσεων.Question για encapsulation.<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
// questions.json
[
{"text": "Capital of France?", "options": ["London", "Paris", "Berlin", "Madrid"], "answer": "B"},
{"text": "2^10?", "options": ["512", "1024", "2048", "1000"], "answer": "B"}
]
// Loading
ObjectMapper mapper = new ObjectMapper();
List bank = mapper.readValue(new File("questions.json"),
mapper.getTypeFactory().constructCollectionType(List.class, Question.class));
Το αρχικό παράδειγμα είχε συντακτικά λάθη (import java.util.random αντί για java.util.Random, Console class που δεν υπάρχει στο standard API, QuestionList[n][0] syntax errors). Ο παραπάνω κώδικας είναι πλήρως λειτουργικός και σύγχρονος.
Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.