Re: Problem with condition
you are using ternary statement(operation), so i guess its a bit confusing.. ,,well the problem is this -- if num1 is greater than or equal to '0' then "ASSIGN" this String value to the variable 'x' which is an integer...
change the variable 'x' to String
Re: Problem with condition
In the code you have.
Code :
int x=(num1>=0)? "positive":"negative";
You are trying to check the value of num1 and return a string and assign it to int x. Change the int x to be String x.
// Json
Re: Problem with condition
Thanks for answering, and thanks for the instruction. Now the problem is, it does not output the "negative" even if I input -1 or any negative number. It always displays "positive".
Re: Problem with condition
Rewrite it to be this.
Code :
String x=(num1 >= 0 ? "positive" : "negative");
I think your parenthesis was in the wrong place.
// Json
Re: Problem with condition
Quote:
Originally Posted by
Json
Rewrite it to be this.
Code :
String x=(num1 >= 0 ? "positive" : "negative");
I think your parenthesis was in the wrong place.
// Json
Thanks for answering, and thanks for the instruction. Now the problem is, it does not output the "negative" even if I input -1 or any negative number. It always displays "positive".
Re: Problem with condition
This code works fine for me.
Code :
int num1 = -1;
String x = (num1 >= 0 ? "positive" : "negative");
System.out.println(x);
// Json
Re: Problem with condition
Quote:
Originally Posted by
Json
Rewrite it to be this.
Code :
String x=(num1 >= 0 ? "positive" : "negative");
I think your parenthesis was in the wrong place.
// Json
This code works fine with me. Thank you Json. :)