Help with an If/Else program...
Hello, im new to java and cant seem to find my mistake in this program :frusty:. Any help would be appreciated. Thank you.
What I want the program to do - if the 'int' is an odd number I want the program to print that it is an odd number, if the 'int' is even I want to computer to state that it is an even number.
public class CheckOddEven {
public static void main(String[] args) {
int number= 49; // Here i set the value of number
System.out.println("The number is " + number);
if (number%2=0) { // Will return true for all integers that are odd because their remainder would be 1
System.out.println("ODD NUMBER");
} else {
System.out.println("EVEN NUMBER");
}
}
}
This is the error report -
Compiling the source code....
$javac /tmp/13469390249656/CheckOddEven.java 2>&1
/tmp/13469390249656/CheckOddEven.java:6: unexpected type
required: variable
found : value
if (number%2=0) {
^
1 error
Re: Help with an If/Else program...
Quote:
cant seem to find my mistake
Please explain what the program does and what you want the program to do.
If you get errors, please copy and paste the full text here.
also please wrap the code in code tags. See: BB Code List - Java Programming Forum
Re: Help with an If/Else program...
Thanks for your help but i found the solution to my problem -
if (number%2=0)
This was incorrect, it should have been -
if (number%2==0)
although i found the answer i dont understand why i have to use == rather than =
can anyone explain it to me please?
Re: Help with an If/Else program...
Quote:
why i have to use == rather than =
They are different operators for different purposes:
= is for assignment
== is to test equality
Re: Help with an If/Else program...