我不允許使用三個以上的打印語句。我嘗試通過創建私有方法,但它沒有成功。如何減少此代碼的冗餘?
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;
}
可使用\ n新行 –
如果此代碼的工作,你應該問的[代碼審查(http://codereview.stackexchange.com/)。 –
我投票結束這個問題作爲題外話,因爲它屬於http://codereview.stackexchange.com/ –