# Logic to check if value of 2 multiple is true or false?

Page 1 of 1

## 8 Replies - 618 Views - Last Post: 02 August 2012 - 09:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=287679&amp;s=108272aba58c6cf2f7b8b724795dc6df&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rjohn5854

Reputation: 3
• Posts: 172
• Joined: 04-October 11

# Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:22 AM

I need logic to check if (for example: 2 * 2 = 4 is true or false.). The two values are random values between 1 and 4.

```import java.util.Random;

public class ComputerTuts {

public static void main(String[] args) {

Random random = new Random();
int num = 1 + random.nextInt(4);
int num1 = 1 + random.nextInt(4);

int result = 0;
boolean correct = true;

System.out.printf("How much is %d times %d", num, num1);
result = num * num1;
System.out.println("Result: " + result);
result = correct;

//Error: Type mismatch: cannot convert from int to boolean

if(result = correct){
System.out.println("Very Good.");
}
else

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Logic to check if value of 2 multiple is true or false?

### #2 jon.kiparsky

• Pancakes!

Reputation: 8829
• Posts: 15,238
• Joined: 19-March 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:25 AM

use the == operator to compare two values and return a boolean result. For example,

```int i = 5;
if (i * i == 25)
System.out.println("double equals returns a boolean");
else
System.out.println("oh no! math is broken!");
```

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:29 AM

Then you can just compare result with the value read. You do not need a boolean correct

### #4 rjohn5854

Reputation: 3
• Posts: 172
• Joined: 04-October 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:34 AM

I'm not asking the user... randomly generating 2 values.

Is this logic correct?

```if(result == num * num1){
System.out.println("Very Good.");
}
else

}

```

Output:
```How much is 3 times 3
Result: 9
Very Good.

```

### #5 jon.kiparsky

• Pancakes!

Reputation: 8829
• Posts: 15,238
• Joined: 19-March 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:39 AM

Looks good to me.

Of course, you shouldn't trust me, I make mistakes all the time. What would you do to test it?

### #6 rjohn5854

Reputation: 3
• Posts: 172
• Joined: 04-October 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:43 AM

Haha, you're being humble. But yea, i have no idea how to test this ...but i tried

```if(result != num * num1){
```

I got the else statement printed out.

```How much is 4 times 1
Result: 4
```

Doesn't look right to me.

This post has been edited by rjohn5854: 02 August 2012 - 07:44 AM

### #7 rjohn5854

Reputation: 3
• Posts: 172
• Joined: 04-October 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:56 AM

Anyone?

### #8 jon.kiparsky

• Pancakes!

Reputation: 8829
• Posts: 15,238
• Joined: 19-March 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 08:36 AM

Not being humble - I really do make mistakes, ask anyone. In this case I'm pretty sure you're good, but it's a good chance to think about testing a function.

The first question is, what sorts of test cases would it take to convince you that this operator returns what you want? You might want to think about classes of values - where might this fail?

Generally, we'd like to test positive and negative and zero values, we'd like to test extremes, and if there are "edge cases" and "corner cases" we want to test those as well. We want to make sure we test both true and false cases - ie, both equal and unequal quantities. Integer equality, frankly, doesn't make a good example, because there aren't really a whole lot of ways to break it, but you can certainly come up with a list of values that will convince you that == does return what you want it to return.
You can and should use this sort of logic any time you're asking "does this do what I want it to do?". Not that you shouldn't ask here as well, but if you've done a little exploration, you can answer a lot of questions for yourself.

Quote

```How much is 4 times 1
Result: 4

```

Doesn't look right to me.

Since you were asking for the negative:

```if(result != num * num1){
```

This is actually correct, in that it's doing what you told it to do. In this case, it's not the case that 4 * 1 is not-equal-to 4. That is, 4 does in fact equal 4 * 1. So it correctly responds as if the test came back false, which it did.

This post has been edited by jon.kiparsky: 02 August 2012 - 08:39 AM

### #9 rjohn5854

Reputation: 3
• Posts: 172
• Joined: 04-October 11

## Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 09:08 AM

Thank You Jon