繁体   English   中英

编译错误

[英]Compiling Error

随附的代码组织不充分。 重新设计它的目标是它具有更精细的结构并避免过量。 为了帮助消除多余的部分,将代码更改为一个名为 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

我认为错误信息很清楚。 验证器希望您的辅助方法不返回任何内容(具有void返回类型),但您的方法返回int
说明确实说辅助方法应该进行打印,因此只需将该代码移动到spending方法中,更改返回类型,就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM