如何在第三种方法中乘以两种不同方法返回的值?

时间:2017-05-20 03:27:59

标签: java methods java.util.scanner return-value

我试图找到股票的买入价。我无法乘以stocksBoughtstockPrice

  • getStocksBought 方法返回已购买的股票数量

  • getStockBoughtPrice 方法在购买时返回一只股票的价格

  • getBuyingPrice 方法通过将getStocksBought返回的值乘以getStockBoughtPrice返回的值

  • 来返回购买价格

Java代码

public static int getStocksBought(Scanner kb){
    System.out.print("Enter the number of stocks bought:");
    int stocksBought = kb.nextInt();
    return stocksBought;
}

public static double getStockBoughtPrice(Scanner kb){
    System.out.print("Enter the price of one stock when bought: ");
    double stockBoughtPrice = kb.nextDouble();
    return stockBoughtPrice;
}

public static double getBuyingPrice(){
    double buyingPrice = getStocksBought() * getStockBoughtPrice();
    return buyingPrice;
}

但是我得到了

Method: getStocksBought required: 
    java.util.Scanner found: no arguments 
    reason: actual and formal argument lists differ in length –

1 个答案:

答案 0 :(得分:4)

您的两种方法都需要Scanner参数(并且您没有传递一个参数)。像,

public static double getBuyingPrice(){
    Scanner kb = new Scanner(System.in);
    double buyingPrice = getStocksBought(kb) * getStockBoughtPrice(kb);
    return buyingPrice;
}