+ 1
Scanner & .nextInt
why is.nextInt required in print with a scanner input variable? eg. Scanner d = new Scanner(System.in); System.out.println(evenOdd(d.nextInt()));
1 Answer
+ 1
Because evenOdd is a method which accepts an int as a parameter, and returns a String.
so when you execute that code it will print some text on the screen, depending on the number you gave.
the method looks like this:
String evenOdd(int number){
if (number % 2 == 0)
return "Even";
else
return "Odd";
return "";
}
Now when you call that method, you pass a value to replace the "number" variable, which requires it to be an int.
Thats why we use nextInt(), because it treats our input as an int.