Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1.  
  2. import java.util.Random;
  3.  
  4. public class QuestionBank
  5. {
  6.     Random question = new Random();
  7.     public int Question()
  8.     {
  9.         return question.nextInt(4);
  10.     }
  11.  
  12.     String questions[] = {
  13.     "The smallest prime number?",
  14.     "What is the area of triangle with base = 4 and height = 3?",
  15.     "What is the area of square with side = 5?",
  16.     "What is the square root of 144?",
  17.     "How many states are in the US?",
  18.     "How many continents are in the world?",
  19.     "In which year did man land on the moon?",
  20.     "How many colors in a rainbow?",
  21.     "How many colors in the US flag?",
  22.     "What is the square of 25?"};
  23.  
  24.     int answers[] = {2,6,25,12,50,7,1969,7,3,5};
  25.  
  26.     boolean CheckAnswers(int useranswer, int questionnumber)
  27.     {
  28.         if(useranswer == answers[questionnumber])
  29.             return true;
  30.         else
  31.             return false;
  32.     }
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. import java.util.Scanner;
  46. import java.util.Random;
  47.  
  48. public class OnlineQuiz {
  49.  
  50.     public static void main(String[] args) {
  51.         // declare variable
  52.         int totalScore = 0;
  53.         Scanner input = new Scanner(System.in);
  54.         Random rng = new Random();
  55.         QuestionBank qb = new QuestionBank();
  56.         int useranswer = 0;
  57.        
  58.         int questionsasked[] = new int[5];
  59.        
  60.         for (int i=0;i<4;i++)
  61.         {
  62.             int nextnumber  = rng.nextInt(9);
  63.             boolean repeated = false;
  64.             for (int j=0;i<4;i++)
  65.             {
  66.                 if(nextnumber == questionsasked[j])
  67.                     repeated = true;
  68.             }
  69.             if(!repeated)
  70.                 questionsasked[i] = nextnumber;
  71.         }
  72.        
  73.         for(int k=0;k<4;k++)
  74.         {
  75.             System.out.println((k+1)+". "+qb.questions[k]);
  76.             useranswer = input.nextInt();
  77.            
  78.             if(qb.CheckAnswers(useranswer, questionsasked[k]))
  79.                 totalScore+=2;
  80.             else
  81.                 totalScore-=1;
  82.         }
  83.        
  84.         System.out.println("\nFINAL SCORE: "+totalScore +"/10");
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement