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.

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 */

Re: Two conditions in a while statement

Quote:

whose product ( * ) is 684 en whose sum ( + ) is 55 with a java program

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.

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.

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!

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.

Re: Two conditions in a while statement

Quote:

Originally Posted by

**jim829** 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...

Re: Two conditions in a while statement

Quote:

Originally Posted by

**jim829** 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?

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

Re: Two conditions in a while statement

Quote:

Originally Posted by

**loki88** 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.

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

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.

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

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

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.

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");

}

}

Re: Two conditions in a while statement

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

kind regards,

Jos

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.

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

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** 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).

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.

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.

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");

}

}

Re: Two conditions in a while statement

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

Re: Two conditions in a while statement

Eureka! I've found the solution.

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?

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