naman1601

A simple Quiz in JAVA.

Aug 21st, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4. import java.util.Random;
  5. public class Quiz {
  6.     public static void main(String[] args) {
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.         Random r = new Random();
  9.         String[] q = {
  10.             "The 1st PM of India?", "The 1st President of India?", "CEO of Google?", "Creator of PageRank?", "Founder of TechNo Times?", "Author of Jack Reacher?", "King of gods?", "Author of The Winner Stands Alone?", "Author of Best Kept Secret?", "Lead actor of Minority Report?"
  11.         };
  12.         String[] a = {
  13.             "Jawaharlal Nehru", "Rajendra Prasad", "Sundar Picchai", "Larry Page", "Naman Chhaparia", "Lee Child", "Indra", "Paulo Coelho", "Jefferey Archer", "Tom Cruise"
  14.         };
  15.         int noRepeat[] = {
  16.             0, 0, 0, 0, 0
  17.         };
  18.         int randomNo = 0;
  19.         int[] prize = {
  20.             0, 1, 2, 3, 4, 5
  21.         };
  22.         int run = 0;
  23.         int prizeInd = 0;
  24.         boolean lose = false;
  25.         boolean askAgain = true;
  26.         String ans = "";
  27.         int nrInd = 0;
  28.         int ansInd = 1000;
  29.         int ct = 10;
  30.         while (run < 5 && lose == false) {
  31.             while (askAgain == true) {
  32.                 randomNo = r.nextInt(10);
  33.                 if (has(noRepeat, randomNo)) askAgain = true;
  34.                 else askAgain = false;
  35.             }
  36.             askAgain = true;
  37.             System.out.print(q[randomNo] + "  ");
  38.             noRepeat[nrInd] = randomNo;
  39.             nrInd += 1;
  40.             try {
  41.                 ans = br.readLine();
  42.             } catch (IOException e) {}
  43.             if (hasit(a, ans)) {
  44.                 ansInd = getInd(a, ans);
  45.                 ct = 1;
  46.             } else {
  47.                 lose = true;
  48.                 ansInd = 1000;
  49.                 ct = 0;
  50.                 System.out.println("\nYou\'ve lost. Wrong answer.");
  51.                 System.out.print("Current points : " + prize[prizeInd]);
  52.             }
  53.             if (ct == 1) {
  54.                 if (ansInd == randomNo) {
  55.                     prizeInd += 1;
  56.                     lose = false;
  57.                     System.out.println("\nCorrect.");
  58.                     System.out.println("Your points : " + prize[prizeInd]);
  59.                 } else {
  60.                     lose = true;
  61.                     ansInd = 1000;
  62.                     ct = 0;
  63.                     System.out.println("\nYou\'ve lost. Wrong answer.");
  64.                     System.out.print("Current points : " + prize[prizeInd]);
  65.                 }
  66.             }
  67.             run += 1;
  68.         }
  69.         if (prize[prizeInd] == 5) {
  70.             System.out.print("\nYou win.");
  71.         } else {
  72.             System.out.print("\nGame Over.");
  73.         }
  74.     }
  75.     public static boolean hasit(String[] hay, String needle) {
  76.         int i = 0;
  77.         int haylen = hay.length;
  78.         boolean retval = false;
  79.         for (i = 0; i < haylen; i++) {
  80.             if (hay[i].equals(needle)) {
  81.                 retval = true;
  82.                 break;
  83.             }
  84.         }
  85.         return retval;
  86.     }
  87.     public static boolean has(int[] hay, int needle) {
  88.         int i = 0;
  89.         int haylen = hay.length;
  90.         boolean retval = false;
  91.         for (i = 0; i < haylen; i++) {
  92.             if (hay[i] == needle) {
  93.                 retval = true;
  94.                 break;
  95.             }
  96.         }
  97.         return retval;
  98.     }
  99.     public static int getInd(String[] hay, String needle) {
  100.         int i = 0;
  101.         int haylen = hay.length;
  102.         int x = 1000;
  103.         for (i = 0; i < haylen; i++) {
  104.             if (hay[i].equals(needle)) {
  105.                 x = i;
  106.                 break;
  107.             }
  108.         }
  109.         return x;
  110.     }
  111. }
Add Comment
Please, Sign In to add comment