好吧,所以我建立了印度卢比的面额柜台。说,如果您输入Rs。 3453,它给出以下输出:

1000卢比笔记:3
500卢比纸币:0
100卢比:4
50卢比注:1
Rs 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);
}
}

最佳答案

可以将... + ... + ...see Javadoc for StringBuilder)组合成多个语句,而不必将字符串构建为形式为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());


通过使用这种方法,可以将任何单独的“ append”语句包装在if块中,以确保在将值添加到字符串之前,该值不为零。

10-05 18:11
查看更多