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
System.out.println("No! Please Try Again");
}
}
8 Replies - 150 Views - Last Post: 02 August 2012 - 09:08 AM
#1
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.
Replies To: Logic to check if value of 2 multiple is true or false?
#2
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
Re: Logic to check if value of 2 multiple is true or false?
Posted 02 August 2012 - 07:29 AM
You'll need a Scanner to read the answer from the user
Then you can just compare result with the value read. You do not need a boolean correct
Then you can just compare result with the value read. You do not need a boolean correct
#4
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?
Output:
Is this logic correct?
if(result == num * num1){
System.out.println("Very Good.");
}
else
System.out.println("No! Please Try Again");
}
Output:
How much is 3 times 3 Result: 9 Very Good.
#5
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?
Of course, you shouldn't trust me, I make mistakes all the time. What would you do to test it?
#6
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
I got the else statement printed out.
Doesn't look right to me.
if(result != num * num1){
I got the else statement printed out.
How much is 4 times 1 Result: 4 No! Please Try Again
Doesn't look right to me.
This post has been edited by rjohn5854: 02 August 2012 - 07:44 AM
#7
Re: Logic to check if value of 2 multiple is true or false?
Posted 02 August 2012 - 07:56 AM
Anyone?
#8
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.
You should also think about this sort of logic as a way to ensure your programs work correctly. For this, google "test-driven development" and "junit" and do some reading.
Doesn't look right to me.
Since you were asking for the negative:
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.
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.
You should also think about this sort of logic as a way to ensure your programs work correctly. For this, google "test-driven development" and "junit" and do some reading.
Quote
How much is 4 times 1 Result: 4 No! Please Try Again
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
Re: Logic to check if value of 2 multiple is true or false?
Posted 02 August 2012 - 09:08 AM
Thank You Jon
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|