As usual, I'm just another highschooler desperate enough to resort to asking the question after lots of googling.
I'm working on one of them programs that can somewhat be applied in real life (but never are). In this case, a change counter that sums up the number of dimes, nickels, quarters and remaining cents and shows them in one sentence.
My problem is, I need to make a proper sentence format that uses commas and an "and" in the end, which I can't manage with my very limited current knowledge.
- Anywhere you see a division of currency is a variable that represents it (cents, dimes etc)
- backupInput is the variable that holds the user's chosen number of cents (anything from 1-100)
The part of my program concerned with what I want to ask is this:
System.out.print(backupInput + ": ");
// Outputting the quarters
if (quarters > 0)
{
if (quarters == 1)
System.out.print("one quarter, ");
else
System.out.print(quarters + " quarters, ");
}
// Outputting the dimes
if (dimes > 0)
{
if (dimes == 1)
System.out.print("one dime, ");
else
System.out.print(dimes + " dimes, ");
}
// Outputting the nickels
if (nickels > 0)
{
if (nickels == 1)
System.out.print("one nickel, ");
else
System.out.print(nickels + " nickels ");
}
// Outputting the cents
if (cents > 0)
{
if (cents == 1)
System.out.print(" one cent");
else
System.out.print(cents + " cents ");
}
The problem is, anything this outputs give the result of something like
7: 1 dime, 2 cents
While it's supposed to be, according to the example format given:
7: 1 dime and 2 cents
65: 2 quarters, 1 dime and one nickel
66: 2 quarters, 1 dime, 1 nickel, and one cent
Keep in mind I tried nearly everything I could think of and in all my tries it was too inefficient and didn't even work in all the cases. I'm not trying to save myself from work here. I just want to learn the concept behind doing it right.
That's about it. The rest of the program works fine and dandy. If any of you could help me, please keep the answer easy to understand for a newbie like me so it's easier to grasp the concept :) Thanks!