I want to have an if-statement checking whether a number is even or odd by using the modulo operator.
The idea is to divide the value by 2 and see if the rest is 1 or 0.
For some reason, my IDE (IntelliJ) tells me I need to have a boolean instead of int but to me, it doesn´t make sense to divide the words true or false with a mathematic operator.
import java.util.Scanner;
public class IsThisNumberEven {
public static void main(String[] args) {
Scanner InputNumber = new Scanner(System.in);
int Value = InputNumber.nextInt();
System.out.print("The number " + Value + "is ");
if (Value % 2 = 1) {
System.out.print("odd.");
} else if (Value % 2 = 0) {
System.out.print("even.");
}
}
}
How do I use the operator modulo on an integer in an if-statement without getting said that the types are incompatible and I need to use boolean.
Aucun commentaire:
Enregistrer un commentaire