2014-04-19 166 views
-5

隨附的代碼組織不當。重做它的目標是它有一個更精細的結構,並遠離過度。爲了幫助消除過量,將代碼轉換爲名爲using的策略,確認兩個參數:掃描儀以獲取舒適感,以及單獨個人姓名的字符串,並打印有關該人賬單的合適數據。你的技巧可以被調用兩次(一次是約翰和一次是簡),以複製第一個代碼的行爲。編譯錯誤

public static void main (String [] args) { 
    Scanner console = new Scanner(System.in); 
    int numBills1 = spending(console, "John"); 
    int numBills2 = spending(console, "Jane"); 
    System.out.println("John needs " + numBills1 + " bills"); 
    System.out.println("Jane needs " + numBills2 + " bills"); 
} 

public static int spending(Scanner console, String name) { 
    System.out.print("How much will " + name + " be spending? "); 
    double amount = console.nextDouble() 
     System.out.println(); 
    int numBills = (int) (amount / 20.0); 
    if (numBills * 20.0 < amount) { 
      numBills++; 
     } 
    return numBills; 
} 

錯誤

Return types do not match. 
expected return type: void 
your return type was: int 
+5

你的問題是什麼?看起來你剛剛發佈了作業問題。 – Barmar

+0

那我提交我的程序時得到的錯誤我想知道爲什麼 – user3552378

+1

你似乎在'double amount ='行中缺少一個分號。 – Barmar

回答

1

我認爲錯誤信息是很清楚的。驗證程序期望您的幫助程序方法不返回任何內容(具有void返回類型),但您的方法返回int
說明確實說幫助方法應該進行打印,所以只需將該代碼移入spending方法,更改返回類型,並且應該沒問題。