2014-04-05 33 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); 
}