Results 1 to 20 of 32
 08212013, 04:19 PM #1Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
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 xtimes 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 */
 08212013, 04:27 PM #2Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,105
 Rep Power
 6
Re: Two conditions in a while statement
whose product ( * ) is 684 en whose sum ( + ) is 55 with a java programJava Code:while (number1 * number2 !=55 && number1 + number2 !=684)
"Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 08212013, 04:28 PM #3
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!
 08212013, 04:29 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,786
 Rep Power
 5
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^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 08212013, 04:35 PM #5
Re: Two conditions in a while statement
&& works like AndGate. It will return true only if both the conditions are true, otherwise it will return false.
 08212013, 04:41 PM #6Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,105
 Rep Power
 6
Re: Two conditions in a while statement
"Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 08212013, 04:43 PM #7Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
 08212013, 04:46 PM #8Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,786
 Rep Power
 5
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 08212013, 04:52 PM #9
Re: Two conditions in a while statement
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!
 08212013, 05:10 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,663
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 08212013, 05:13 PM #11Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,786
 Rep Power
 5
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 }
should take a look at De Morgan's laws which I find quite useful from time to time.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 08222013, 11:07 AM #12Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
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;
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"); } }
 08222013, 11:22 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,663
 Blog Entries
 7
 Rep Power
 21
Re: Two conditions in a while statement
Have a close look at line#27: = assigns and == compares.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 08222013, 11:28 AM #14Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
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.
 08222013, 11:33 AM #15Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
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"); } }
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; 08222013 at 11:36 AM.
 08222013, 11:34 AM #16Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
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.
 08222013, 11:40 AM #17Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
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; 08222013 at 11:42 AM.
 08222013, 11:57 AM #18Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
Re: Two conditions in a while statement
Still missing some steps inside your loop. Made the same mistake as before...
 08222013, 02:45 PM #19Member
 Join Date
 Aug 2013
 Posts
 8
 Rep Power
 0
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?? } }}
Last edited by loki88; 08222013 at 02:47 PM.
 08222013, 03:26 PM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,663
 Blog Entries
 7
 Rep Power
 21
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*(55n) == 684 >
n*n  55*n == 684 >
n*n  55*n + 27.5^2 == 27.5^2  684 (completing the square) >
(n27.5)^2 == 72.25 >
n27.5 == +/ 8.5 > n == 19  n == 36
We don't need no steenkin' loops ...
kind regards,
JosLast edited by JosAH; 08222013 at 03:49 PM. Reason: stupid me, I can't even get those numbers right ...
cenosillicaphobia: the fear for an empty beer glass
Similar Threads

How to avoid many if else conditions
By Addymails in forum New To JavaReplies: 9Last Post: 03222012, 12:39 PM 
Problem in while conditions
By davie89 in forum New To JavaReplies: 2Last Post: 03202012, 12:37 AM 
Asking for a username with conditions
By ScentOfAWookie in forum New To JavaReplies: 2Last Post: 03202009, 05:53 AM
Bookmarks