2014-04-05 32 views
0

好吧,我已經爲印度貨幣盧比建立了一個面額計數器。說,如果你輸入Rs。 3453,它給出了這樣的輸出:從結果中消除零(Java)

盧比1000注:3
500個盧比筆記:0
100個盧比筆記:4
盧比50注:1
盧比20個音符:0
10個盧比筆記: 0
5盧比紙幣:0
2枚盧比硬幣:1
1枚盧比硬幣:1

但我想這個輸出和消除所有的零,

盧比1000注:3個
100個盧比筆記:4個
盧比50注:1
2枚盧比硬幣:1個
盧比1枚硬幣:1

這是我的代碼:

import java.io.*; 
import javax.swing.JOptionPane; 

public class denom { 
public static void main(String[] args) throws IOException{ 

    String totalRsString; 
    int totalRs; 
    totalRsString = JOptionPane.showInputDialog(null, "Enter amount to be converted", "Denomination Conversion", JOptionPane.INFORMATION_MESSAGE); 
    totalRs = Integer.parseInt(totalRsString); 
    //Calculations begin here 
    int thousand, fh, h, f, twenty, t, fi, tw, o; 
    thousand = totalRs/1000; 
    int bal = totalRs - (1000*thousand); 
    fh = bal/500; 
    bal = bal - (500*fh); 
    h = bal/100; 
    bal = bal - (100 * h); 
    f = bal/50; 
    bal = bal - (50*f); 
    twenty = bal/20; 
    bal = bal - (20*twenty); 
    t = bal/10; 
    bal = bal-(10*t); 
    fi = bal/5; 
    bal = bal - (5*fi); 
    tw = bal/2; 
    bal = bal - (2*tw); 
    o = bal/1; 
    bal = bal - (1*o); 
    //End of calculation 
    //Print work. 
    JOptionPane.showMessageDialog(null, "Total Entered is Rs." + totalRsString + "\n" +  "\nThousand rupee notes: " + thousand + "\nFive Hundred Notes: " + fh + "\nHundred notes: " + h + "\nFifty notes: " + f + "\nTwenty notes: " + twenty + "\nTen notes: " + t + "\nFive notes: " + fi + 
    "\nTwo coins: " + tw + "\nOne coins: " + o); 
} 
} 

回答

1

您可以使用StringBuildersee Javadoc for java.lang.StringBuilder)在多個語句中將其組裝起來,而不是將您的字符串構建爲表單... + ... + ...的單個表達式。例如,像這樣:

JOptionPane.showMessageDialog(null, "foo: " + 17 + "\n" + "bar" + 18 + "\n"); 

可以寫成這樣:

StringBuilder message = new StringBuilder(); 
message.append("foo: ").append(17).append("\n"); 
message.append("bar: ").append(18).append("\n"); 
JOptionPane.showMessageDialog(null, message.toString()); 

通過使用這種方法,你可以用任何個人在一個if - 阻塞,使「追加」聲明確保該值在將其添加到字符串之前爲非零值。

+0

謝謝,這個人幫了我很多。你的方法更方便。謝謝Ruakh。 –

+0

@MissionCoding:不客氣! – ruakh

0

您需要逐步構建輸出字符串。如果該特定輸入的硬幣或音符的相應數量等於零,則應跳過最後一個字符串中的該元素。

喜歡的東西:

string output = "Total Entered is Rs." + totalRsString + "\n"; 
    if(thousand == 0){ 
     output += "\nThousand rupee notes: " + thousand; 
    } 
    /* Here you will do the same for the rest of notes and coins */ 

JOptionsPane.showMessageDialog(null, output); 

嗯,這是一個懶惰的解決方案。但是你可以用更優雅的方式來實現它。

0

嘗試減少正在創建的變量數量。看看那些可以重複使用的。

StringBuilder sb = new StringBuilder(); 
    int totalRs = 5500; 
    int bal = totalRs; 
    int numNotes =0; 

    if ((numNotes =bal/1000) > 0){ 
    sb.append("Rs 1000 notes: " + numNotes + "\n"); 
    bal = bal - (1000 * numNotes); 
    } 
    if ((numNotes =bal/500) > 0) { 
    sb.append("Rs 500 notes: " + numNotes + "\n"); 
    bal = bal - (500 * numNotes); 
    } 
1

作爲替代方案,可以考慮使用enum舉行valuekindcountCurrency每種形式:

private enum Kind { 

    Coins, Notes 
}; 

private enum Currency { 

    // … 
    Ten(10, Kind.Notes), 
    Five(5, Kind.Notes), 
    Two(2, Kind.Coins), 
    One(1, Kind.Coins); 

    private int value; 
    private Kind kind; 
    private int count; 

    private Currency(int value, Kind kind) { 
     this.value = value; 
     this.kind = kind; 
    } 
}; 

然後你convert()方法可以通過Currency實例進行迭代,並返回一個List<Currency>只包括非零計數。

private static List<Currency> convert(int amount) { 
    List<Currency> list = new ArrayList<>(); 
    int balance = amount; 
    for (Currency currency : Currency.values()) { 
     // update currency.count 
     // update balance; 
     if (currency.count != 0) { 
      list.add(currency); 
     } 
    } 
    return list; 
} 

最後,您可以循環雖然List<Currency>打印結果:

List<Currency> list = convert(3453); 
for (Currency currency : list) { 
    System.out.println("Rs " 
     + currency.value + " " 
     + currency.kind + ": " 
     + currency.count); 
}