一个JAVA小程序 急

来源:百度知道 编辑:UC知道 时间:2024/05/27 17:57:38
编写一个程序,把变量n的初始值设置为5814,然后利用除法运算和取余运算把变量N的每一位数字都抽出并打印出来,输出结果为:

N=5814

The digits of n are 5,8,1,4

提示:用N/1000抽取n的千位数,利用N%=1000把N的千位数移走。

public class PrintNum {

public static void main(String[] args) {
int n = 5814;
int i, j ,k = 0;
i = n % 1000; //i = 814
j = i % 100; //j = 14
k = j % 10; //k = 4
System.out.println("n 原来的值为: " + n);
System.out.println("n 各个数位上的数为: ");
System.out.println("千位为: " + n/1000 + " 百位为: " + i/100 + " 十位为: " + j/10 + " 个位为: " + k);

}

}

这个程序没有什么意义,如果你想输入任意数字然后判断每一位上的数字是多少还好.

..我记得String有个把字符串转换成字符数组的方法.
还必须要用除法?呵呵..楼上的.人家一定要用除法肯定有人家的用处.

用处?反正殊途同归,在转换成整形就是了,何必拘泥于形式呢

public class Test{
public static void main(String args[]){
int n=5814;
String tmp=n+"";
String result;
for(int i=0;i<tmp.length;i++){
result=tmp.charAt(i)+" ";
}
System.out.println(result);
}
}

Integer.toString(5814);

纯数学问题