Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. 1) What is 1+2 = ...?
  2. a) 2
  3. b) 3
  4. c) 0
  5. d) 1
  6. b
  7. ...
  8.  
  9. import java.io.File;
  10. import java.util.Scanner;
  11. import javax.swing.JOptionPane;
  12.  
  13. public class GameQuiz
  14. {
  15. public static void main(String[] args) {
  16. String[] data = new String[120];
  17. String question = "";
  18. int count = 0;
  19. try{
  20. Scanner inFile = new Scanner(new File("questions.txt"));
  21. while (inFile.hasNext()){
  22. data[count] = inFile.nextLine();
  23. count++;
  24. }
  25. inFile.close();
  26. }catch(Exception e){
  27. e.printStackTrace();
  28. }
  29.  
  30. int choice = 0, numCorrect = 0, i = 0;
  31. char correctAnswer = ' ', correct = ' ';
  32. String answer= "";
  33. String answerChoices[] = {"a", "b", "c", "d"};
  34. for (int j = 0; j < 20; j++)
  35. {
  36. answer = "";
  37. for (i = 0; i < 5; i++)
  38. {
  39. answer += data[i+(j*6)] + "n";
  40. }
  41. correctAnswer = data[i+(j*6)].charAt(0);
  42.  
  43. choice = JOptionPane.showOptionDialog(null, answer, "Quiz Game",
  44. 0, 3, null, answerChoices, null);
  45.  
  46.  
  47. if (choice == 0) correct = 'a';
  48. if (choice == 1) correct = 'b';
  49. if (choice == 2) correct = 'c';
  50. if (choice == 3) correct = 'd';
  51.  
  52. if (correct == correctAnswer)
  53. {
  54. numCorrect ++;
  55. JOptionPane.showMessageDialog(null, "Correct!");
  56. }
  57. else JOptionPane.showMessageDialog(null, "Sorry.n"+
  58. "The correct answer was "+correctAnswer);
  59. }
  60. JOptionPane.showMessageDialog(null, "You have "+numCorrect+"/20 correct answers.");
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement