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

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
*/

So what problems are you having? What have you tried?

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.

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) {
} 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.

Here's my version of program

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");
}
}
}
}
}
It's unable to quit the program when I entered my choice as 3, number above 3 and number below 0

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

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