2016-10-27 90 views
-2

我被困在一個練習中,這真的讓我難以忍受。基本上在這個代碼中,我希望這個傳遞方法的結果調用increaseQuantity方法,雖然我相信它很簡單,但編譯時我經常遇到一個錯誤。增加數量錯誤 - 解決方案?

錯誤消息讀取:類產品

方法increaseQuantity不能給予給定的類型;需要int;發現:沒有參數;理由:實際和正式參數列表的長度不同。

我已經從字面上撞上了這面牆,所以任何幫助將不勝感激十倍!

public int delivery (int id, int amount) 
{ 
    int index = 0; 
    boolean searching = true; 
    Product myproduct = stock.get(0); 
    while(searching && index < myproduct.getID()){ 
     int Products = myproduct.getID(); 
     if(Products == id) { 
      searching = false; 
     } 
     else { 
      index++; 
     } 
    } 
    if(searching) { 
     return 0; 
    } 
    else { 
     return myproduct.increaseQuantity(); 
    } 
} 

非常感謝那些迴應。

+2

好吧,你需要添加一個'int'參數,如錯誤 – AxelH

+0

看起來你'increaseQuantity()'方法需要一個'int'作爲參數,但你給它什麼都不說。此外,如果您在編程時「實際上」撞牆,您需要與工作場所的健康和安全代表聯繫。 – jsheeran

+2

也許你可以分享你的產品類代碼,或者至少增加incrementQuantity()方法的源代碼 – alainlompo

回答

0

位寬和代碼缺失,但如果你的問題是代碼不編譯,這裏你有一個工作的例子,在代碼裏面有解釋。

public class Main { 
    public static void main(String[] args) throws Exception { 
     // TODO do something 
    } 

    // must fill it somehow 
    private List<Product> stock; 

    // method signature wants an int as return type!!  
    public int delivery(int id, int amount) { 
     int index = 0; 
     boolean searching = true; 
     Product myproduct = stock.get(0); 
     while (searching && index < myproduct.getID()) { 
      int Products = myproduct.getID(); 
      if (Products == id) { 
       searching = false; 
      } else { 
       index++; 
      } 
     } 
     if (searching) { 
      // this already returns an int (0) 
      return 0; 
     } else { 
      // your error was here because 
      // increaseQuantity does not return an int * 
      return myproduct.increaseQuantity(); // | 
     }           // | 
                // | 
    }            // | 
                // | 
}             // | 
                // | 
class Product {         // | 
    // just my guess        // | 
    private int ID;        // | 
                // | 
    public int getID() {       // | 
     return this.ID;       // | 
    }            // | 
                // | 
    // here must be your problem, now this  // | 
    // method signature says to return an int!! // |  
    public int increaseQuantity() {    // | 
     // ↑ this indicates return type!    | 
     // *-------------------------------------------* 

     // add here your method logic 
     return 0; // change this! 
    } 

}