[問題] 請問一題java

作者: OCEANSAE (OCEANSAE)   2019-03-25 10:46:34
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
String str;
Scanner input = new Scanner(System.in);
while(true) {
str = input.nextLine();
if (str == "0")
break;
else {
String arr[] = str.split(" ");
int total = 0;
for (int i = 0; i < arr.length; i++) {
if ('0' <= arr[i].charAt(0) && arr[i].charAt(0) <= '9') {
int int0 = Integer.parseInt(arr[i],10);
int reversedInt = reverse(int0);
total += reversedInt;
}
}
String tot = String.format("%04d", total);
System.out.println(tot);
}
}
input.close();
}
public static int reverse(int integer) {
int result=0;
while(true)
{
int n= integer % 10;
result=result*10+n;
integer = integer / 10;
if (integer == 0)
{
break;
}
}
System.out.println(result);
}
}
想請如果出以下錯誤:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at Q1.reverse(Q1.java:30)
at Q1.main(Q1.java:18)
請問為什麼這樣寫出來的result不是整數?
以下是題目:
請根據以下規則計算結果:
*將句子中的數字取出來反轉並加總;
*加總結果若超過千位數則取到千位數,不到千位數則以 0 補
之。
輸入說明:輸入多筆測資,直到輸入 0 結束程式。每筆測資是一個
句子。
輸出說明:根據上述規則輸出最後結果。
作者: pttworld (批踢踢世界)   2019-03-25 11:39:00
你的if後面加了分號
作者: OCEANSAE (OCEANSAE)   2019-03-26 12:52:00
謝謝你,我又修改了程式碼,卻發現了其它問題 我等等改個內文問不知道可不可以在麻煩大大解答
作者: pttworld (批踢踢世界)   2019-03-26 16:37:00
你的reverse沒有回傳值
作者: OCEANSAE (OCEANSAE)   2019-03-26 18:22:00
發現了,謝謝!
作者: qrtt1 (有些事,有時候。。。)   2019-03-26 18:48:00
你有使用 debugger 去看自己的問題嗎?

Links booklink

Contact Us: admin [ a t ] ucptt.com