帮忙看怎么输入数了,以后就什么都不输出,求解答

import java.util.Scanner;
public class Ex5_2 {
public static int sumDIgits(long n){ int tmp;
int result = 0;
Long m = new Long(n);
while(n>0){
tmp = m.intValue();
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);
Ex5_2 ex = new Ex5_2();
System.out.println(ex.sumDIgits(sc.nextLong()));
}

}

凤凰求蛊
浏览 149回答 3
3回答

互换的青春

代码有问题是死循环 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()));}}

料青山看我应如是

while(n>0){tmp = m.intValue();result = tmp%10 +result;tmp = tmp/10;}你这是个死循环啊,n永远大于0希望可以帮助你

POPMUISE

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()));}}
打开App,查看更多内容
随时随地看视频慕课网APP