Ok, this is my solution with an infinite loop that breaks when user 0 is entered or when the program ends:
import java.util.Scanner;
class Oddeven {
public static void main(String[] arguments) {
Scanner inputChar = new Scanner(System.in);
int i;
while (true) {
System.out.println("Type in any integer:");
if(!inputChar.hasNextInt()) {
System.out.println("Not a number");
inputChar.next();
} else {
i = inputChar.nextInt();
if (i != 0) {
if (i % 2 == 0)
System.out.println(i + " is even");
else {
System.out.println (i + " is odd") ;
}
} else {
System.out.println("Zeros are not allowed, bye!");
break;
}
}
}
}
}
Ask if you do not understand something, I hope this helps, welcomes!
source
share