3 回答
TA贡献1797条经验 获得超6个赞
代码有问题是死循环 while(n>0){循环中没有重新给n赋过值总是大于0会一直循环的。
这样就行了
import java.util.Scanner;
public class Ex5_2 {
public static int sumDIgits(long n){
int result = 0;
while(n>0){
result = (int) (n%10 +result);
n = n/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}
}
TA贡献1772条经验 获得超8个赞
while(n>0){
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
你这是个死循环啊,n永远大于0
希望可以帮助你
TA贡献1765条经验 获得超5个赞
import java.util.Scanner;
public class Ex5_2 {
public int sumDIgits(long n){
int tmp;
int result = 0;
Long m = new Long(n);
tmp = m.intValue();
while(tmp>0){
result = tmp%10 +result;
tmp = tmp/10;
}
return result;
}
public static void main(String[] args) {
System.out.println("Please Input a long number:");
Scanner sc = new Scanner(System.in);
System.out.println(Ex5_2.sumDIgits(sc.nextLong()));
}
}
- 3 回答
- 1 关注
- 148 浏览
添加回答
举报