自由输入一个任意三位数,正确分解它的各位,十位,百位(用java)

2024-11-27 20:31:00
推荐回答(2个)
回答1:

public class Tes {
public static void fun1(int num){
//比较粗暴
int i1 = num/100;
int i2 = (num-i1*100)/10;
int i3 = num-i1*100-i2*10;
System.out.println("百位:"+i1);
System.out.println("十位:"+i2);
System.out.println("个位:"+i3);
}

public static void fun2(int num){
String string = String.valueOf(num);
System.out.println("百位:"+string.charAt(0));
System.out.println("十位:"+string.charAt(1));
System.out.println("个位:"+string.charAt(2));
}
public static void main(String[] args) {
fun1(123);
System.out.println("-----------------");
fun2(123);
}

}

回答2:

思路:除100结果是百位,原数减去百位值除10是十位,再减最后是个位