从结果中消除零(Java)

时间:2014-04-05 17:17:14

标签: java swing if-statement zero

好的,所以我为印度货币卢比建立了一个面额计数器。比如说,如果你输入卢比。 3453,它给出了这个输出:

卢比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);
}
}

4 个答案:

答案 0 :(得分:1)

您可以使用... + ... + ...see Javadoc for java.lang.StringBuilder)将字符串汇总到多个语句中,而不是将字符串构建为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块中的语句,用于确保在将值添加到字符串之前该值为非零值。

答案 1 :(得分:1)

作为替代方案,请考虑使用enumvalue的每种形式保留kindcountCurrency

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);
}

答案 2 :(得分: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);

嗯,这是一个懒惰的解决方案。但是你需要以更优雅的方式实现它。

答案 3 :(得分: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);
   }