# Thread: checking multiplication from user help!!

1. ## checking multiplication from user help!!

Ok I am suppose to write a code where a user inputs 2 numbers and then has to input the value of the 2 numbers when they are multiplied. If user input is wrong it is suppose to pop the question up again to let the user try again. When my program runs it does the calculations but when it displays its a continuous display and wont let the user input another value. here is my code:

*Code*
import java.util.Scanner;
public class Hw5 {

public static void main(String[] args) {
int n1,n2,n3,n4;
Scanner input = new Scanner(System.in);

System.out.print("Enter a integer: ");
n1 = input.nextInt();

System.out.print("Enter a integer: ");
n2 = input.nextInt();

System.out.print("What is "+n1+" multiplied by "+n2+" equal?");
n3 = input.nextInt();
n4 = n1 * n2;

while(true){
boolean T = checkResult(n3, n4);
if (T==true) System.out.println("Very good!");
}

}

else return false;
}
}

2. I have fixed my loop error but still don't know how to get it to prompt for another user input if answer is incorrect...
PHP Code:
```import java.util.Scanner;
public class Hw5 {

public static void main(String[] args) {
int n1,n2,n3,n4;
Scanner input = new Scanner(System.in);

System.out.print("Enter a integer: ");
n1 = input.nextInt();

System.out.print("Enter a integer: ");
n2 = input.nextInt();

System.out.print("What is "+n1+" multiplied by "+n2+" equal?");
n3 = input.nextInt();
n4 = n1 * n2;

while(true){
boolean T = checkResult(n3, n4);
if (T==true){
System.out.println("Very good!");
break;
}
else System.out.println("Incorrect please try again."); break;
}

}

else return false;
}
}```

3. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
7
In the block where you say "incorrect, try again", add a "continue;", it will do another iteration of the loop.

4. I disagree. Formulate terminating conditions correctly and you'll never require to continue or break from a loop.

db

5. import java.util.*;
class ProductCheck
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n1,n2,p1,p2;
try
{
System.out.print("Enter an integer:");
n1=scan.nextInt();
System.out.print("Enter another integer:");
n2=scan.nextInt();
for(;;)
{
System.out.print("Enter product of "+n1+"&"+n2+":");
p1=scan.nextInt();
p2=n1*n2;
if(p1==p2)
{
System.out.println("Very Good");
System.exit(0);
}
else
System.out.println("The product you entered is incorrect. Please try again...");
continue;
}
}
catch(NumberFormatException e)
{
System.out.println("You have entered an invalid Integer. Program exits.");
}
}
}
__________________________________________________ ______________________

6. Moderator
Join Date
Apr 2009
Posts
13,371
Rep Power
24
Will you stop handing people code, especially when it may well be not very good (since you say you're a beginner).

7. i put a continue statement on the else block and it prints that message out infinity time, i have to manually terminate it...I tried to test BZwap code, had so many errors and for the for loop, I am confused on what the condition would be?

8. Moderator
Join Date
Apr 2009
Posts
13,371
Rep Power
24
You will need to stick the part where you ask for the answer inside your loop, otherwise how do you expect it to ask again?

9. DOH! ty tolls, was just a copy and paste away from correctness and I cannot believe that flew over my head

#### Posting Permissions

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