|
| 1 | +package MCQProject; |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.FileReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class QuestionAndAnswer { |
| 8 | + BufferedReader br = null; |
| 9 | + String line = ""; |
| 10 | + String commaCsv = ","; |
| 11 | + public static double trueAns = 0.0; |
| 12 | + public static double falseAns = 0.0; |
| 13 | + Scanner sc = new Scanner(System.in); |
| 14 | + |
| 15 | + public void MCQ(int choice) throws IOException { |
| 16 | + switch (choice) { |
| 17 | + //file location for MCQ sets |
| 18 | + case 1: { |
| 19 | + try { |
| 20 | + { |
| 21 | + String fileLocation = "C:\\\\Users\\\\Victor Swuan Yee Lin\\\\IdeaProjects\\\\MCQ_Demo\\\\src\\\\csv\\\\Java.csv"; |
| 22 | + |
| 23 | + br = new BufferedReader(new FileReader(fileLocation)); |
| 24 | + //BufferedReader is reading csv file and save it into line String variable |
| 25 | + //This process is iterating end of csv files (MCQ Questions) |
| 26 | + while ((line = br.readLine()) != null) { |
| 27 | + //Reading csv file using BufferedReader |
| 28 | + |
| 29 | + String[] QueAndAns = line.split(commaCsv); |
| 30 | + //Showing MCQ Question to the user in console screen |
| 31 | + |
| 32 | + System.out.println("Please answer with Capital Letter !!! \n"); |
| 33 | + System.out.println(QueAndAns[0] + "\n" + QueAndAns[1] + "\n" + QueAndAns[2] + "\n" + QueAndAns[3] + "\n" + QueAndAns[4]); |
| 34 | + System.out.print("Type your Answer Here: "); |
| 35 | + String Ans = sc.next(); //Requesting answer from the user |
| 36 | + if (QueAndAns[5].equals(Ans)) {//Testing user answer is correct or not |
| 37 | + System.out.println("Your answer is correct."); |
| 38 | + System.out.println("*******************************************************"); |
| 39 | + trueAns++; |
| 40 | + } else { |
| 41 | + System.out.println("Your answer is Wrong !!!"); |
| 42 | + System.out.println("*******************************************************"); |
| 43 | + falseAns++; |
| 44 | + |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (Exception e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } finally { |
| 51 | + br.close(); |
| 52 | + } |
| 53 | + } |
| 54 | + break; |
| 55 | + case 2 : { |
| 56 | + try { |
| 57 | + { |
| 58 | + String fileLocation = "C:\\\\Users\\\\Victor Swuan Yee Lin\\\\IdeaProjects\\\\MCQ_Demo\\\\src\\\\csv\\\\HTML.csv"; |
| 59 | + |
| 60 | + br = new BufferedReader(new FileReader(fileLocation)); |
| 61 | + //BufferedReader is reading csv file and save it into line String variable |
| 62 | + //This process is iterating end of csv files (MCQ Questions) |
| 63 | + while ((line = br.readLine()) != null) { |
| 64 | + //Reading csv file using BufferedReader |
| 65 | + |
| 66 | + String[] QueAndAns = line.split(commaCsv); |
| 67 | + //Showing MCQ Question to the user in console screen |
| 68 | + |
| 69 | + System.out.println("Please answer with Capital Letter !!! \n"); |
| 70 | + System.out.println(QueAndAns[0] + "\n" + QueAndAns[1] + "\n" + QueAndAns[2] + "\n" + QueAndAns[3] + "\n" + QueAndAns[4]); |
| 71 | + System.out.print("Type your Answer Here: "); |
| 72 | + String Ans = sc.next(); //Requesting answer from the user |
| 73 | + if (QueAndAns[5].equals(Ans)) {//Testing user answer is correct or not |
| 74 | + System.out.println("Your answer is correct."); |
| 75 | + System.out.println("*******************************************************"); |
| 76 | + trueAns++; |
| 77 | + } else { |
| 78 | + System.out.println("Your answer is Wrong !!!"); |
| 79 | + System.out.println("*******************************************************"); |
| 80 | + falseAns++; |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } finally { |
| 88 | + br.close(); |
| 89 | + } |
| 90 | + } |
| 91 | + break; |
| 92 | + case 3 : { |
| 93 | + try { |
| 94 | + { |
| 95 | + String fileLocation = "C:\\\\Users\\\\Victor Swuan Yee Lin\\\\IdeaProjects\\\\MCQ_Demo\\\\src\\\\csv\\\\PHP.csv"; |
| 96 | + |
| 97 | + br = new BufferedReader(new FileReader(fileLocation)); |
| 98 | + //BufferedReader is reading csv file and save it into line String variable |
| 99 | + //This process is iterating end of csv files (MCQ Questions) |
| 100 | + while ((line = br.readLine()) != null) { |
| 101 | + //Reading csv file using BufferedReader |
| 102 | + |
| 103 | + String[] QueAndAns = line.split(commaCsv); |
| 104 | + //Showing MCQ Question to the user in console screen |
| 105 | + |
| 106 | + System.out.println("Please answer with Capital Letter !!! \n"); |
| 107 | + System.out.println(QueAndAns[0] + "\n" + QueAndAns[1] + "\n" + QueAndAns[2] + "\n" + QueAndAns[3] + "\n" + QueAndAns[4]); |
| 108 | + System.out.print("Type your Answer Here: "); |
| 109 | + String Ans = sc.next(); //Requesting answer from the user |
| 110 | + if (QueAndAns[5].equals(Ans)) {//Testing user answer is correct or not |
| 111 | + System.out.println("Your answer is correct."); |
| 112 | + System.out.println("*******************************************************"); |
| 113 | + trueAns++; |
| 114 | + } else { |
| 115 | + System.out.println("Your answer is Wrong !!!"); |
| 116 | + System.out.println("*******************************************************"); |
| 117 | + falseAns++; |
| 118 | + |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } catch (Exception e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } finally { |
| 125 | + br.close(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + } |
| 131 | + //restart the user score when the user re-answer the MCQ questions |
| 132 | + public void reset(){ |
| 133 | + trueAns = 0.0; |
| 134 | + falseAns = 0.0; |
| 135 | + } |
| 136 | +} |
0 commit comments