+ 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()));

31st Dec 2016, 8:12 PM
Jameson
Jameson - avatar
1 Réponse
+ 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.
31st Dec 2016, 9:08 PM
Uran Kajtazaj
Uran Kajtazaj - avatar