352 % 10-->2 (n % 10 --> 个位)
352 /10 -->35
35%10 -->5
35 / 10 -->3
3 % 10 -->3
3 / 10 --> 0(终止符)
逆序输出做法:
1.
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number ;
number = in.nextInt();
do{
int digit = number % 10;
<span style="color:#ff0000;">System.out.print(digit);</span>
number = number / 10;
} while (number > 0);
<span style="color:#ff0000;">System.out.println();</span>
}
}
红字为新
缺点:输入700,输出007
2.
public class Hello {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number ;
number = in.nextInt();
<span style="color:#3366ff;">int result = 0;</span>
do{
int digit = number % 10;
<span style="color:#3366ff;">result = result * 10+ digit;
</span> <span style="color:#ff0000;">System.out.print(digit);</span>
number = number / 10;
} while (number > 0);
System.out.println();
<span style="color:#ff0000;">System.out.println(result);</span>
}
}
输入700,输出007,7
假如程序一开始用while
while (number > 0)
{
int digit = number % 10;
result = result * 10+ digit;
System.out.print(digit);
number = number / 10;
}
System.out.println();
System.out.println(result);
输入700无问题,但是输入0出现问题,下星期再介绍