Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) What is 1+2 = ...?
- a) 2
- b) 3
- c) 0
- d) 1
- b
- ...
- import java.io.File;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class GameQuiz
- {
- public static void main(String[] args) {
- String[] data = new String[120];
- String question = "";
- int count = 0;
- try{
- Scanner inFile = new Scanner(new File("questions.txt"));
- while (inFile.hasNext()){
- data[count] = inFile.nextLine();
- count++;
- }
- inFile.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- int choice = 0, numCorrect = 0, i = 0;
- char correctAnswer = ' ', correct = ' ';
- String answer= "";
- String answerChoices[] = {"a", "b", "c", "d"};
- for (int j = 0; j < 20; j++)
- {
- answer = "";
- for (i = 0; i < 5; i++)
- {
- answer += data[i+(j*6)] + "n";
- }
- correctAnswer = data[i+(j*6)].charAt(0);
- choice = JOptionPane.showOptionDialog(null, answer, "Quiz Game",
- 0, 3, null, answerChoices, null);
- if (choice == 0) correct = 'a';
- if (choice == 1) correct = 'b';
- if (choice == 2) correct = 'c';
- if (choice == 3) correct = 'd';
- if (correct == correctAnswer)
- {
- numCorrect ++;
- JOptionPane.showMessageDialog(null, "Correct!");
- }
- else JOptionPane.showMessageDialog(null, "Sorry.n"+
- "The correct answer was "+correctAnswer);
- }
- JOptionPane.showMessageDialog(null, "You have "+numCorrect+"/20 correct answers.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement