0
Help! I need to generate a user input (int) and get the last digit of the number. Please help.
I need to generate a user input: Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); That's not a problem ;) but I need to know get the last digit of the number. How can I do that? Please help me. Thank you so much. I can follow you if you lead me to the right way. ;)
2 Antworten
+ 2
You can use:
int lastDigit = number % 10;
System.out.println(lastDigit);
+ 1
do it like this, hope this help !!
public static int lastDigit(int n)
{
return (n % 10);
}
System.out.println(lastDigit(n));