亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

幫忙看怎么輸入數了,以后就什么都不輸出,求解答

幫忙看怎么輸入數了,以后就什么都不輸出,求解答

C C++
鳳凰求蠱 2022-07-14 19:11:58
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()));}}
查看完整描述

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()));
}
}


查看完整回答
反對 回復 2022-07-18
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

while(n>0){
tmp = m.intValue();
result = tmp%10 +result;
tmp = tmp/10;
}
你這是個死循環啊,n永遠大于0
希望可以幫助你

查看完整回答
反對 回復 2022-07-18
?
POPMUISE

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()));
}
}


查看完整回答
反對 回復 2022-07-18
  • 3 回答
  • 1 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號