Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Addition Quiz

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Addition Quiz

    I need assistance for this question
    • When user choose option 1: he will enter the number to practice
    • When user choose option 2: the random number from 1 to 10 will be chosen to practice
    • When user choose option 3: the program ends



    /*Output
    Menu:
    1. Choose a number to practice
    2. Practice with random number
    3. Quit
    Your Choice: 2

    You will be working on: 8
    1 + 8 = 9
    Correct!
    2 + 8 = 10
    Correct!
    3 + 8 = 12
    Wrong!
    4 + 8 = 12
    Correct!
    5 + 8 = 13
    Correct!
    6 + 8 = 14
    Correct!
    7 + 8 = 15
    Correct!
    8 + 8 = 15
    Wrong!
    9 + 8 = 17
    Correct!
    10 + 8 = 18
    Correct!
    Your Score: 8/10

    Menu:
    1. Choose a number to practice
    2. Practice with random number
    3. Quit
    Your Choice: 1

    Enter a number of your choice: 7
    1 + 7 = 7
    Wrong!
    2 + 7 = 10
    Wrong!
    3 + 7 = 10
    Correct!
    4 + 7 = 11
    Correct!
    5 + 7 = 12
    Correct!
    6 + 7 = 13
    Correct!
    7 + 7 = 14
    Correct!
    8 + 7 = 15
    Correct!
    9 + 7 = 16
    Correct!
    10 + 7 = 20
    Wrong!
    Your Score: 7/10

    Menu:
    1. Choose a number to practice
    2. Practice with random number
    3. Quit
    Your Choice: 3
    */


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Addition Quiz

    So what problems are you having? What have you tried?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Addition Quiz

    After the menu, I used if-else-if statement to execute different method.
    I created a variable int choice, so when choice is 1, it prompted user to enter a number.
    When the choice is 2, I used Math.random(10)+1 to select a random number from 1 to 10.
    The program will exit when choice is 3.

    The few issues I faced were to print whether if the result is correct or wrong, and to display the right result.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Addition Quiz

    You seem to be on the right track at the moment, so I will give you some basic pseudo code to guide you.

    		if (userAnswer == properAnswer) {
    			 print "correct", increment correctAnswer counter.
    		} else {
    			 print "incorrect" !
    		}

    All of this can go inside of a loop, preferable a for loop, where you can use the loops counter to perform the calculations of the correct answer.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Addition Quiz

    Here's my version of program

    import java.util.*;

    public class Addition {
    public static void main (String[]args) {
    Scanner sc = new Scanner (System.in);
    int choice = 0;

    while (choice<3) {
    System.out.println("Menu:");
    System.out.println("1. Enter a number to practice");
    System.out.println("2. Practice with a random number");
    System.out.println("3. Quit");
    System.out.print("Your choice: ");
    choice = sc.nextInt();
    int addNum = 0;

    if (choice == 1) {
    //Prompt user for the number to practice on
    System.out.print("\nEnter a number to practice: ");
    addNum = sc.nextInt();
    }
    else if (choice == 2) {
    Random number = new Random();
    addNum = number.nextInt(10)+1;
    System.out.print("\nYou will be quizzed on: " +addNum +"\n");
    }
    int score = 0;

    for (int num=1; num<=10; num++){
    System.out.print(num +" X " +addNum +" = ");
    int userInput = sc.nextInt();
    int addAns = num*addNum;

    if (addAns == userInput) {
    System.out.print("Correct!\n");
    score++;
    }

    else {
    System.out.print("Wrong!\n");
    }
    }
    System.out.print("Your score: " +score +"/10\n\n");
    }
    }
    }
    It's unable to quit the program when I entered my choice as 3, number above 3 and number below 0
    Last edited by javaneedhelp; October 9th, 2011 at 12:20 AM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Addition Quiz

    import java.util.*;
     
    public class Addition { // begin class 
    public static void main (String[]args) { // begin main
    Scanner sc = new Scanner (System.in);
    int choice = 0;
     
    while (choice<3) { // begin while
    System.out.println("Menu:");
    System.out.println("1. Enter a number to practice");
    System.out.println("2. Practice with a random number");
    System.out.println("3. Quit");
    System.out.print("Your choice: ");
    choice = sc.nextInt();
    int addNum = 0;
     
    if (choice == 1) { // begin if
    //Prompt user for the number to practice on
    System.out.print("\nEnter a number to practice: ");
    addNum = sc.nextInt();
    } // end if
    else if (choice == 2) { // begin else if
    Random number = new Random();
    addNum = number.nextInt(10)+1;
    System.out.print("\nYou will be quizzed on: " +addNum +"\n");
    } // end else if
    int score = 0; 
     
    for (int num=1; num<=10; num++){ // begin for
    System.out.print(num +" X " +addNum +" = ");
    int userInput = sc.nextInt();
    int addAns = num*addNum;
     
    if (addAns == userInput) { // begin if
    System.out.print("Correct!\n");
    score++;
    } // end if
     
    else { // begin else
    System.out.print("Wrong!\n");
    } // end else
    } // end for
    System.out.print("Your score: " +score +"/10\n\n");
    } // end while
    } // end main
    }  // end program

    -1 is less than 3. That's one part of it.

Similar Threads

  1. AppInventor Quiz App
    By FA2 in forum The Cafe
    Replies: 4
    Last Post: October 10th, 2011, 03:03 PM
  2. Addition
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 14th, 2011, 10:49 PM
  3. Creating a quiz (really basic doubt)
    By Ayush in forum Object Oriented Programming
    Replies: 2
    Last Post: November 8th, 2010, 12:55 PM
  4. Quiz application
    By JonoF in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 10th, 2010, 06:06 AM
  5. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM