2016-10-27 89 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! 
    } 

}