Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree2Likes

Thread: Two conditions in a while statement

  1. #1
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Two conditions in a while statement

    Good day,

    I am trying to find two numbers whose product is 684 en whose sum is 55 with a java program. (19 and 36 is the answer).
    With the following code, It finds other numbers. It seems like the program does not respect the second condition of the "while" statement.

    Java Code:
    /* This is a small program that must find the numbers whose product is 684 and whose sum is 55  */
    import java.util.Random; 
    import static java.lang.System.out; 
    public class dice { 
    
    	public static void main(String[] args) {
    	Random myRandom = new Random(); 
    	int number1 = 0, number2 =0; 
    	int Count =1; 
    	
    	while (number1 * number2 !=55 && number1 + number2 !=684){
    		number1 = myRandom.nextInt(50) + 1; 
    		number2 = myRandom.nextInt(50) + 1;
    		Count= Count + 1; 
    		out.print(number1); 
    		out.print(" ");
    		out.println(number2);
    		} 
    	out.print(number1+number2); 
    	out.println(" rolled");
    	out.println("it took "+ Count +" times to find the answer"); // it took x-times to find the correct value'
    	}
    	}
    /* it seems if the first condition (!=55) is true, he skips the second condition. I need to find a way to get two conditions into the while statement */

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Two conditions in a while statement

    whose product ( * ) is 684 en whose sum ( + ) is 55 with a java program
    Java Code:
    while (number1 * number2 !=55 && number1 + number2 !=684)
    Somewhere you got something mixed up because your problem description and this line of code are the exact reverse of each other.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Two conditions in a while statement

    That's exactly what your code is set up to do. As soon as one of the boolean expressions is false, the while loop will stop executing. I suggest working through the logic with a bunch of example numbers until you understand what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Two conditions in a while statement

    In your program you the while loop will continue as long as both conditions are true. So if one if the conditions returns false, the loop will terminate. So if you find two numbers which sum to 55 the loop will terminate. If you find two numbers whose product is 684, the loop will terminate.

    Regards,
    Jim

    Edit: Gimbal2 wins!
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Two conditions in a while statement

    && works like And-Gate. It will return true only if both the conditions are true, otherwise it will return false.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Two conditions in a while statement

    Quote Originally Posted by jim829 View Post
    Edit: Gimbal2 wins!
    We're all winners friend, its a beautiful day after all. But I seriously spam too much, I'm going to work on restricting myself. I don't always need to get the first post in ;) That what's new? page is just so tempting...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Quote Originally Posted by jim829 View Post
    In your program you the while loop will continue as long as both conditions are true. So if one if the conditions returns false, the loop will terminate. So if you find two numbers which sum to 55 the loop will terminate. If you find two numbers whose product is 684, the loop will terminate.

    Regards,
    Jim

    Edit: Gimbal2 wins!
    Sorry, just started with Java.

    What can I use then instead of 'while' to loop the rolling until both the sum and product are true?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Two conditions in a while statement

    Also, if your interested, the sum of the two numbers is odd. Which means that one of the numbers must be even and the other odd. So this should help you in making selections or even in generating the numbers.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Two conditions in a while statement

    Quote Originally Posted by loki88 View Post
    Sorry, just started with Java.

    What can I use then instead of 'while' to loop the rolling until both the sum and product are true?
    What else have you tried? Recommended reading: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Think through the logic of what you're trying to do. Look over the explanation that other people have given you of what your code's doing, and step through your own code to prove it to yourself. Then think about how your code is different from what you actually want to do, and the answer should pop out at you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Two conditions in a while statement

    You're looking for two particular numbers m and n such that:

    n*m == 684 && n+m == 55

    If the above conditon isn't true, you want to keep on iterating, so this must be true:

    !(n*m == 684 && n+m == 55)

    And this is where De Morgan hits in: !(A && B) <==> !A || !B, so this must be true:

    n*m != 684 || n+m != 55

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Two conditions in a while statement

    I also assume you might be new to programming. So you should look at some truth tables using && and ||. Here is the quick run thru.

    Java Code:
    // Using &&, a thru d are either boolean values or logical expressions
    boolean v = a && b && c && d;
    // v will also be true as long as all a,b,c,d.. are true.  If one is false, v is false.
    //using  ||
    boolean v = a || b || c || d;
    // v will be false only if all a,b,c,d are false.  If one is true, v is true.
    
    //It is easy to write some programs and play with these.  And remember that the following constructs are equivalent.
    
    int n, m;
    boolean result = n < m;
    if (result) {
      //do something
    }
    
    // or more common version
    if (n < m) {
      // do something
    }
    Once you feel comfortable with the above you
    should take a look at De Morgan's laws which I find quite useful from time to time.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Hello all.

    Thank you for your responses. I am still figuring out. It's true these are my first steps into programming.

    @Jim29

    I tried to group the two conditions (sum and product in one boolean operator, but I get the error I can't use && with an INT variable)
    I tried this

    Java Code:
    boolean result = number1 && number2;
    Then I tried to give the boolean result a value by an if function. My code is stil not working but I do not quite understand why.
    I've read about De Morgan but it's still to complicated for my I think.


    Java Code:
    /* This is a small program that must find the numbers whose product is 684 and whose sum is 55  */
    import java.util.Random; 
    import static java.lang.System.out; 
    public class findnumbers{ 
    
    	public static void main(String[] args) {
    	Random myRandom = new Random(); 
    	int number1 = 0, number2 =0; 
    	int Count =1; 
    	int sum =number1+number2;
    	int product = number1*number2;
    	boolean result; // Eclipse gives me the warning this variable is not used. Though I use it below??
    
    
    	if((sum != 55) && (product !=684))  { // when the sum and product does not match 55 or 684 then result = false
    		result = false; 
    			} 
    	
    	do
    	{
    	number1 = myRandom.nextInt(50) + 1; 
    	number2 = myRandom.nextInt(50) + 1;
    	Count= Count + 1; 
    	out.print(number1); 
        out.print(" ");
        out.println(number2);
    	} while (result = false); // if the result is false, keep on looping
    	
    	
    	out.print(sum); 
    	out.print(" ");
    	out.print(product); 
    	out.println(" rolled");
    	out.println("it took "+ Count +" rolls"); 
    }
    	}

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Two conditions in a while statement

    Have a close look at line#27: = assigns and == compares.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    JPete is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Just a question. Is your goal to play around with loops with multiple conditions or is your goal to find the actual numbers?

    If those logic laws are too difficult to understand, why don't you take a step backwards and try to simplify your solution. I don't understand why you would use 2 random numbers. I would suggest you use the fact that that the sum is 55 to generate your input number. Use an integer value x you increment by one each iteration for the first number and y =55 - x for the second number. By imposing x+y=55 when generating your 2 numbers, you no longer have to check for it in your loop condition.

  15. #15
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Thanks. Didn't notice that. Sharp eyes!

    However, it keeps looping now, even if he finds the correct numbers

    Java Code:
    /* This is a small program that must find the numbers whose product is 684 and whose sum is 55  */
    import java.util.Random; 
    import static java.lang.System.out; 
    public class CopyOfdice { 
    
    	public static void main(String[] args) {
    	Random myRandom = new Random(); 
    	int number1 = 0, number2 =0; 
    	int Count =1; 
    	int sum =number1+number2;
    	int product = number1*number2;
    	boolean result = false; // Eclipse gives me the warning this variable is not used. Though I use it below??
    
    
    	if((sum == 55) && (product ==684))  { // when the sum and product does not match 55 or 684 then result = false
    		result = true; 
    			} 
    	
    	do
    	{
    	number1 = myRandom.nextInt(50) + 1; 
    	number2 = myRandom.nextInt(50) + 1;
    	Count= Count + 1; 
    	out.print(number1); 
        out.print(" ");
        out.println(number2);
    	} while (result == false); // if the result is false, keep on looping
    	
    	
    	out.print(sum); 
    	out.print(" ");
    	out.print(product); 
    	out.println(" rolled");
    	out.println("it took "+ Count +" rolls"); 
    }
    	}
    Quote Originally Posted by JPete View Post
    Just a question. Is your goal to play around with loops with multiple conditions or is your goal to find the actual numbers?

    If those logic laws are too difficult to understand, why don't you take a step backwards and try to simplify your solution. I don't understand why you would use 2 random numbers. I would suggest you use the fact that that the sum is 55 to generate your input number. Use an integer value x you increment by one each iteration for the first number and y =55 - x for the second number. By imposing x+y=55 when generating your 2 numbers, you no longer have to check for it in your loop condition.
    Good question. I would say both.
    As my goal is to learn Java, I think it would not harm if I understand how to do loops with multiple conditions and how to find those numbers.

    I was using reading Java for dummies and just made myself an exercise to find two numbers with a certain product or sum based on a program in the book (throwing dices with a loop).
    Last edited by loki88; 08-22-2013 at 11:36 AM.

  16. #16
    JPete is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Two conditions in a while statement

    That's because you don't recalculate result inside your loop, but keep working with the original value.

  17. #17
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Two conditions in a while statement

    I moved the if function inside the loop now, however I got the same results. It keeps looping forever.

    Java Code:
    /* This is a small program that must find the numbers whose product is 684 and whose sum is 55  */
    import java.util.Random; 
    import static java.lang.System.out; 
    public class findnumbers{ 
    
    	public static void main(String[] args) {
    	Random myRandom = new Random(); 
    	int number1 = 0, number2 =0; 
    	int Count =1; 
    	int sum =number1+number2;
    	int product = number1*number2;
    	boolean result = false;
    
    	do
    	{
    	number1 = myRandom.nextInt(50) + 1; 
    	number2 = myRandom.nextInt(50) + 1;
    	Count= Count + 1; 
    	out.print(number1); 
        out.print(" ");
        out.println(number2);
        if((sum == 55) && (product ==684))  { // when the sum and product does not match 55 or 684 then result = true
    		result = true; 
    			} 
    	} while (result == false); // if the result is false, keep on looping
    	
    	
    	out.print(sum); 
    	out.print(" ");
    	out.print(product); 
    	out.println(" rolled");
    	out.println("it took "+ Count +" rolls"); 
    }
    	}
    Last edited by loki88; 08-22-2013 at 11:42 AM.

  18. #18
    JPete is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Still missing some steps inside your loop. Made the same mistake as before...

  19. #19
    loki88 is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Two conditions in a while statement

    Eureka! I've found the solution.

    Java Code:
    /* This is a small program that must find the numbers whose product is 684 and whose sum is 55  */
    import java.util.Random; 
    import static java.lang.System.out; 
    public class findingnumbers{ 
    
    	public static void main(String[] args) {
    	Random myRandom = new Random(); 
    	int number1 = 0, number2 =0; 
    	int Count =1; 
    	int sum = number1 + number2;
    	int product = number1*number2;
    	boolean result = false; 
    	
    		 
    	while (result == false) {
    		number1 = myRandom.nextInt(50) + 1; 
    	    number2 = myRandom.nextInt(50) + 1;
    	    Count = Count +1;
    	    out.print(number1);
    	    out.print(" ");
    	    out.println(number2);
    	    if((number1+number2 != 55) || (number1*number2 != 684))  { //had to change sum and product to calcs
    		 	result = false;
    		 			        	 		
    	 }  else {
               		result = true;}
    	}
    	
    	if(result==true){
    	out.print("the sum is ");
    	out.print(number1+number2); 
    	out.print(" and ");
    	out.print("the product is ");
    	out.println(number1*number2); 
    	out.println("it took "+ Count +" rolls"); 
    	out.println(sum); // for testing purposes only. For some reason, sum and product are always zero??
    	out.print(product); // for testing purposes only. For some reason, sum and product are always zero??
    	}
    	}}
    For some reason, I had to change my sum and product variables also in calculations. For a strange reason, at the end they are just zero. How comes that?
    Last edited by loki88; 08-22-2013 at 02:47 PM.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Two conditions in a while statement

    Why doesn't anyone apply a bit of math 101 anymore? find n and m such that n+m == 55 and n*m == 684, so

    n*(55-n) == 684 -->
    n*n - 55*n == -684 -->
    n*n - 55*n + 27.5^2 == 27.5^2 - 684 (completing the square) -->
    (n-27.5)^2 == 72.25 -->
    n-27.5 == +/- 8.5 --> n == 19 || n == 36

    We don't need no steenkin' loops ...

    kind regards,

    Jos
    Last edited by JosAH; 08-22-2013 at 03:49 PM. Reason: stupid me, I can't even get those numbers right ...
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. How to avoid many if else conditions
    By Addymails in forum New To Java
    Replies: 9
    Last Post: 03-22-2012, 12:39 PM
  2. Problem in while conditions
    By davie89 in forum New To Java
    Replies: 2
    Last Post: 03-20-2012, 12:37 AM
  3. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 05:53 AM

Posting Permissions

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