문제

Okay so I have built a denomination counter for the Indian currency rupees. Say, if you enter Rs. 3453, it gives this output:

Rs 1000 notes: 3
Rs 500 notes: 0
Rs 100 notes: 4
Rs 50 notes: 1
Rs 20 notes: 0
Rs 10 notes: 0
Rs 5 notes: 0
Rs 2 coins: 1
Rs 1 coin: 1

But I want this output and eliminate all the zeros,

Rs 1000 notes: 3
Rs 100 notes: 4
Rs 50 notes: 1
Rs 2 coins: 1
Rs 1 coin: 1

Here's my code:

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);
}
}
도움이 되었습니까?

해결책

Rather than building your string as a single expression of the form ... + ... + ..., you can use a StringBuilder (see Javadoc for java.lang.StringBuilder) to assemble it across several statements. For example, something like this:

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

can be rewritten like this:

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

By using this approach, you can wrap any of the individual "append" statements in an if-block that makes sure the value is nonzero before adding it to the string.

다른 팁

As an alternative, consider using an enum to hold the value, kind and count for each form of Currency:

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

Then your convert() method can iterate through the Currency instances and return a List<Currency> that includes only non-zero counts.

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

Finally, you can loop though the List<Currency> to print the result:

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

You need to build the output string step-by-step. If the corresponding number of coins or notes for that specific input is equal to zero, you should skip that element in the final string.

Something like:

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

Well, this is a lazy solution. But it's up to you to implement it in a more elegant way.

try reducing the number of variables you are creating. See the ones which can be reused.

   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);
   }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top