Results 1 to 20 of 32
- 08-21-2013, 05:19 PM #1
Member
- 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 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 */
- 08-21-2013, 05:27 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-21-2013, 05: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-21-2013, 05:29 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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 JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-21-2013, 05:35 PM #5
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.
- 08-21-2013, 05:41 PM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Two conditions in a while statement
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-21-2013, 05:43 PM #7
Member
- Join Date
- Aug 2013
- Posts
- 8
- Rep Power
- 0
- 08-21-2013, 05:46 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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 JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-21-2013, 05: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-21-2013, 06:10 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-21-2013, 06:13 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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 JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-22-2013, 12:07 PM #12
Member
- 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"); } }
- 08-22-2013, 12:22 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Two conditions in a while statement
Have a close look at line#27: = assigns and == compares.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-22-2013, 12:28 PM #14
Member
- 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.
- 08-22-2013, 12:33 PM #15
Member
- 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; 08-22-2013 at 12:36 PM.
- 08-22-2013, 12:34 PM #16
Member
- 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.
- 08-22-2013, 12:40 PM #17
Member
- 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; 08-22-2013 at 12:42 PM.
- 08-22-2013, 12:57 PM #18
Member
- 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...
- 08-22-2013, 03:45 PM #19
Member
- 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; 08-22-2013 at 03:47 PM.
- 08-22-2013, 04:26 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosLast edited by JosAH; 08-22-2013 at 04:49 PM. Reason: stupid me, I can't even get those numbers right ...
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
How to avoid many if else conditions
By Addymails in forum New To JavaReplies: 9Last Post: 03-22-2012, 01:39 PM -
Problem in while conditions
By davie89 in forum New To JavaReplies: 2Last Post: 03-20-2012, 01:37 AM -
Asking for a username with conditions
By ScentOfAWookie in forum New To JavaReplies: 2Last Post: 03-20-2009, 06:53 AM
Bookmarks