# checking multiplication from user help!!

• 05-09-2011, 08:30 AM
dpspine420
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;
}
}
• 05-09-2011, 11:22 AM
dpspine420
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...
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;                 }                 }         public static boolean checkResult (int studentAnswer, int correctAnswer){                         if(studentAnswer == correctAnswer)return true;                                 else return false;         } }```
• 05-09-2011, 12:28 PM
Solarsonic
In the block where you say "incorrect, try again", add a "continue;", it will do another iteration of the loop.
• 05-09-2011, 01:45 PM
DarrylBurke
I disagree. Formulate terminating conditions correctly and you'll never require to continue or break from a loop.

db
• 05-09-2011, 03:08 PM
BZwap
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.");
}
}
}
__________________________________________________ ______________________