Reputation: 65
Can anyone tell me if im doing something wrong ?
public class Jafntekkijafnt
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
if (a == b) (b == c) (c == a) System.out.println("equal");
else System.out.println("not equal");
}
}
It's java programming
I'm trying create a program with three integers that prints equal if all are equal and not equal if they are not.
I know its the 8th line but don't how to get it right.
Upvotes: 0
Views: 2943
Reputation: 746
You need to use &&
between the brackets ()
:
(a==b) && (b==c) && (c==a)
Upvotes: 0
Reputation: 2783
Try this:
if(a == b && a == c && b == a && b == c && c == a && c == b){
...
}
Quick and easy.
Upvotes: 0
Reputation: 10026
Not that it is strictly necessary in the one-line case, but you should also include brackets around your 'if' and 'else' statements. It's bad practice not to use them because it can lead to some unsuspected behavior. I know personally that I've gotten burned in the past over this issue. Plus it makes your code easier to read which is invaluable.
My code is below:
public class ThreeIntTest {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
if ((a == b) && (b == c)) {
System.out.println("The three integers are equal.");
} else {
System.out.println("The three integers are not equal.");
}
} }
The &&
logical operator will tell the 'if statement' if both a==b
AND b==c
evaluate as true, if one or both are not true, then the statement is made false and you go directly to your 'else statement.'
Used here is the transitivity property of relations. Which says that if a = b and b = c, then it follows that a = c. This is the most effective way that I can think of comparing three integers.
Upvotes: 0
Reputation: 2475
You have got the brackets a little bit wrong and you need to use the AND (&&) operator.
Upvotes: 0
Reputation: 15552
THere is a problem with your "if" syntax. It should be
if (a == b && b == c && c == a) {
//...
}
Note that youare using primitives here and not object. If you were using Integer instead of int then the == would check if the objects were teh same and not if there values were the same. In this case you would need to use
if (a.equals(b) && b.equals(c) && c.equals(a)) {
//...
}
The && means logical and and is explained here
Upvotes: 0
Reputation: 19766
instead of if (a==b)(b==c)(c==a)
use
if (a==b && b==c)
Note there is no need to write c==a
if a==b
and b==c
hold
Upvotes: 10