Results 1 to 9 of 9
  1. #1
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default 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 System.out.println("Incorrect please try again.");
    }

    }
    public static boolean checkResult (int studentAnswer, int correctAnswer){

    if(studentAnswer == correctAnswer)return true;

    else return false;
    }
    }

  2. #2
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    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;
    		}
    	
    	}
    	public static boolean checkResult (int studentAnswer, int correctAnswer){
    	
    		if(studentAnswer == correctAnswer)return true;
    		
    		else return false;
    	}
    }

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    In the block where you say "incorrect, try again", add a "continue;", it will do another iteration of the loop.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

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

    db

  5. #5
    BZwap's Avatar
    BZwap is offline Member
    Join Date
    May 2011
    Location
    Lucknow
    Posts
    8
    Rep Power
    0

    Default

    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.");
    }
    }
    }
    __________________________________________________ ______________________
    You can use this code. Do tell me about any feedback about this post. I am a Jbeginner.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Will you stop handing people code, especially when it may well be not very good (since you say you're a beginner).

  7. #7
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    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. #9
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

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

Similar Threads

  1. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 03:41 AM
  2. Multiplication code
    By bomboy in forum Eclipse
    Replies: 2
    Last Post: 12-28-2010, 01:11 PM
  3. Multiplication Table
    By BillyB in forum New To Java
    Replies: 17
    Last Post: 12-24-2010, 06:58 AM
  4. Need help with checking user input
    By hmwtd in forum Java Gaming
    Replies: 0
    Last Post: 10-28-2010, 10:07 PM
  5. Help with Multiplication
    By phil028 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 07:39 PM

Posting Permissions

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