a=i / 10000 % 10;//求出万位上的数
b=i / 1000 % 10; //求出千位上的数
c=i / 100 % 10; //求出百位上的数
d=i / 10 % 10; //求出十位上的
e=i % 10; //求出个位上的数
这里举个例子:
package jiujiucfb.com.All;
import java.util.Scanner;
public class demo_test {
public static void main(String[] args){
//方法一将数的各个数字分离
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
//获取输入的数字n
int n = sc.nextInt();
//先算5位数
int a=n / 10000 % 10;//求出万位上的数
int b=n / 1000 % 10; //求出千位上的数
int c=n / 100 % 10; //求出百位上的数
int d=n / 10 % 10; //求出十位上的
int e=n % 10; //求出个位上的数
System.out.print("万位="+a);
System.out.print("千位="+b);
System.out.print("百位="+c);
System.out.print("十位="+d);
System.out.print("个位="+e);
}
}
}
输出结果:
23232
万位=2 千位=3 百位=2 十位=3 个位=2
Process finished with exit code -1
拓展笔记❀❀❀:
String ans = i + ""; //数字转化为字符串
String reverse = new StringBuffer(ans).reverse().toString(); //逆转字符串
除reverse其他全是必须语。
(译语)
Buffer:起缓冲作用
reverse:逆向 / 背面 / 相反 / 倒退