• October 8th, 2011, 11:23 AM
javaneedhelp
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
1. Choose a number to practice
2. Practice with random number
3. Quit

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!

1. Choose a number to practice
2. Practice with random number
3. Quit

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!

1. Choose a number to practice
2. Practice with random number
3. Quit
*/
• October 8th, 2011, 12:33 PM
newbie
So what problems are you having? What have you tried?
• October 8th, 2011, 01:20 PM
javaneedhelp
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.
• October 8th, 2011, 05:01 PM
newbie
You seem to be on the right track at the moment, so I will give you some basic pseudo code to guide you.

Code java:

``` 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.
• October 9th, 2011, 12:10 AM
javaneedhelp
Here's my version of program

Quote:

import java.util.*;

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

while (choice<3) {
System.out.println("1. Enter a number to practice");
System.out.println("2. Practice with a random number");
System.out.println("3. Quit");
choice = sc.nextInt();

if (choice == 1) {
//Prompt user for the number to practice on
System.out.print("\nEnter a number to practice: ");
}
else if (choice == 2) {
Random number = new Random();
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();

System.out.print("Correct!\n");
score++;
}

else {
System.out.print("Wrong!\n");
}
}
```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```