Results 1 to 2 of 2
  1. #1
    askinne2 is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default Help with quiz program

    I am writing a quiz program for fun/studying. It's supposed..

    1 - Ask for how many questions you need.
    2 - Ask for the questions you want to ask.
    3 - Ask for the answer to those questions.
    4 - Randomly ask a question from the ones you provided. Compare the answer you give to the correct answer, tell you if you were right or wrong, and count how many you got right and wrong.
    5 - Ask if you want another question.

    All is working well, except once you answer the first question when it asks if you want another question it's adding the current question into it and not letting you answer if you want to have another question or not.

    Java Code:
    import java.util.*;
    
    public class QuizMaster
    {
    	public static void main(String[] args)
    	{
    		
    		int qAmount = 0;
    		int correctAnswers = 0;
    		int incorrectAnswers = 0;
    		String answer = null;
    		
    		Random rand = new Random();
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.print("How many questions do you need? ");
    		qAmount = Integer.parseInt(scan.nextLine());
    		
    		String[] arrQuestions = new String[qAmount];  //new array of questions
    		String[] arrAnswers = new String[qAmount];  //new array of answers
    		
    		for(int x=0; x < qAmount; x++)
    		{
    			System.out.print("Enter question: ");
    			arrQuestions[x] = scan.nextLine();
    		}
    		
    	
    		for(int t=0; t< qAmount; t++)
    		{
    			System.out.print("Enter answer for " + arrQuestions[t] + " -->");
    			arrAnswers[t] = scan.nextLine();
    		}
    		
    		String choice = "yes";
    		
    		while(choice == "yes")
    		{
    			int a = rand.nextInt(qAmount - 1) + 0; 
    			System.out.print(arrQuestions[a]);
    			answer = scan.nextLine();
    			
    			if(answer.compareTo(arrAnswers[a]) == 0)
    			{
    				System.out.println("Correct");
    				correctAnswers++;
    			}
    			if(answer.compareTo(arrAnswers[a]) != 0)
    			{
    				System.out.println("Sorry, that is incorrect");
    				incorrectAnswers++;
    			}
    			
    			System.out.print("Want another question? (yes/no): ");
    		}
    		
    		System.out.println("Questions you got right: " + correctAnswers + ".");
    		System.out.println("Questions you got wrong: " + incorrectAnswers + ".");
    	}
    }
    Sample run:
    Java Code:
    How many questions do you need? 3
    Enter question: Whats 5 + 2?
    Enter question: Am I alive?
    Enter question: Whats my name?
    Enter answer for Whats 5 + 2? -->7
    Enter answer for Am I alive? -->yes
    Enter answer for Whats my name? -->Alex
    Am I alive?yes
    Correct
    Want another question? (yes/no): Whats 5 + 2?non
    Sorry, that is incorrect
    Want another question? (yes/no): Whats 5 + 2?no
    Sorry, that is incorrect
    Want another question? (yes/no): Whats 5 + 2?
    Thanks for the help!

  2. #2
    askinne2 is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    Nevermind! I feel kinda silly for missing..
    Java Code:
    choice = scan.nextLine();
    ...after the "do you want another question?" line.

    haha, Thanks anyways!

Similar Threads

  1. Quiz Time
    By rjuyal in forum Advanced Java
    Replies: 1092
    Last Post: 06-23-2012, 09:55 AM
  2. quiz program using access as database
    By miinnesh in forum AWT / Swing
    Replies: 5
    Last Post: 03-05-2010, 12:30 PM
  3. quiz program using access as database
    By miinnesh in forum JDBC
    Replies: 0
    Last Post: 03-05-2010, 06:54 AM
  4. Help with quiz
    By khanzaman in forum AWT / Swing
    Replies: 10
    Last Post: 01-19-2009, 08:45 AM
  5. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •