2017-03-12 137 views
0

我不允許使用三個以上的打印語句。我嘗試通過創建私有方法,但它沒有成功。如何減少此代碼的冗餘?

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; 
} 
+0

可使用\ n新行 –

+1

如果此代碼的工作,你應該問的[代碼審查(http://codereview.stackexchange.com/)。 –

+0

我投票結束這個問題作爲題外話,因爲它屬於http://codereview.stackexchange.com/ –

回答

0

怎麼樣使用新的生產線\n

System.out.println("John needs " + numBills1 + " bills\nJane needs " + numBills2 + " bills"); 

你也並不真的需要空的打印。

public static void main(String[] args) { 
    //... 
    System.out.println("John needs " + numBills1 + " bills" + "\n" + "Jane needs " + numBills2 + " bills"); 
    //---------------------------------------------------------^^^---------- 
} 

,並在你的方法spending你可以改變print:刪除

System.out.println(); 
0

你有兩種解決方案,你可以,如果你願意,你可以使用\n正好連接你的字符串中使用剛剛到println

public static int spending(Scanner console, String name) { 
    System.out.println("How much will " + name + " be spending? "); 
    //--------------^^--------------------------------------------- 

並刪除:

System.out.println(); 
0

因爲它已經提到您可以連接兩個字符串,並打印在梅索德一個System.out.println("Text here");

你也不必使掃描儀上的爭論在梅索德,你可以簡單地說,之前主要梅索德,類型:

static Scanner output = new Scanner (System.in); 

這樣,您就可以使用掃描儀在整個(種)整個程序是這樣的:

public static int spending(String name) 
{ 
     System.out.println("How much will " + name + " be spending? "); 
     double amount = output.nextDouble(); 

     int numBills = (int) (amount/20.0); 
     if (numBills * 20.0 < amount) 
     { 
      numBills++; 
     } 
     return numBills; 
} 
0

像這樣的事情

public static void main(String[] args) { 
    while (true) { 
     Scanner console = new Scanner(System.in); 
     System.out.print("The name : "); 
     String name = console.nextLine(); 
     System.out.print("How much will " + name + " be spending? "); 
     double amount = console.nextDouble(); 
     System.out.println(name + " needs " + calculateBills(amount) + " bills"); 
    } 
} 

private static int calculateBills(double amount) { 
    int numBills = (int) (amount/20.0); 
    if (numBills * 20.0 < amount) { 
     numBills++; 
    } 
    return numBills; 
} 
0

你似乎想圍捕。一種方法是

int numBills = (int) Math.ceil(amount/20); 
1

您可以嘗試將輸出文本存儲在實例變量中,然後對所有輸出使用一個打印語句。

private static String output=""; 
public static void main(String[] args) { 
    Scanner console = new Scanner(System.in); 
    spending(console, "John"); 
    spending(console, "Jane"); 
    System.out.println(output); 
} 
public static void spending(Scanner console, String name) { 
    System.out.print("How much will " + name + " be spending? "); 
    double amount = Double.parseDouble(console.nextLine()); 
    int numBills = (int) (amount/20.0); 
    if (numBills * 20.0 < amount) { 
     numBills++; 
    } 
    output += name + " needs "+numBills+" bills\n"; 
}