Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class QuestionBank
- {
- Random question = new Random();
- public int Question()
- {
- return question.nextInt(4);
- }
- String questions[] = {
- "The smallest prime number?",
- "What is the area of triangle with base = 4 and height = 3?",
- "What is the area of square with side = 5?",
- "What is the square root of 144?",
- "How many states are in the US?",
- "How many continents are in the world?",
- "In which year did man land on the moon?",
- "How many colors in a rainbow?",
- "How many colors in the US flag?",
- "What is the square of 25?"};
- int answers[] = {2,6,25,12,50,7,1969,7,3,5};
- boolean CheckAnswers(int useranswer, int questionnumber)
- {
- if(useranswer == answers[questionnumber])
- return true;
- else
- return false;
- }
- }
- import java.util.Scanner;
- import java.util.Random;
- public class OnlineQuiz {
- public static void main(String[] args) {
- // declare variable
- int totalScore = 0;
- Scanner input = new Scanner(System.in);
- Random rng = new Random();
- QuestionBank qb = new QuestionBank();
- int useranswer = 0;
- int questionsasked[] = new int[5];
- for (int i=0;i<4;i++)
- {
- int nextnumber = rng.nextInt(9);
- boolean repeated = false;
- for (int j=0;i<4;i++)
- {
- if(nextnumber == questionsasked[j])
- repeated = true;
- }
- if(!repeated)
- questionsasked[i] = nextnumber;
- }
- for(int k=0;k<4;k++)
- {
- System.out.println((k+1)+". "+qb.questions[k]);
- useranswer = input.nextInt();
- if(qb.CheckAnswers(useranswer, questionsasked[k]))
- totalScore+=2;
- else
- totalScore-=1;
- }
- System.out.println("\nFINAL SCORE: "+totalScore +"/10");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement