javafor循环计算一个三位数,在每一次循环时用表达式表示出当前三位数的个位数、十位数、百位数,各是多少

2024-12-29 11:18:54
推荐回答(1个)
回答1:

public class Test {
public static void main(String[] args) {
int num = 751;
String str = String.valueOf(num);
for (int i = 0; i < str.length(); i++)
if (i == 0)
System.out.println("百位:" + str.charAt(i));
else if (i == 1)
System.out.println("十位:" + str.charAt(i));
else
System.out.println("个位:" + str.charAt(i));
}
}