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!
Sometimes it’s easier to break the problem down a bit. What I’d probably do is create an array of strings: [“2 quarters”, “1 dime”, “1 nickel”, “one cent”]
Then look at how many (valid) array elements I had and loop through N-1 of the N elements, outputting the lines separated by commas. Then (assuming that there’s more than one array element) output “and” followed by the last array element.
There are other ways that are “more efficient”, but I’ve been in this biz 40 years, and I’ll take “easy to understand and works” over “efficient” any day.
(Of course, this isn’t the only way to skin this cat, and probably there are others that are equally simple, but that’s for you and others to invent.)
(Note that it’s critical to avoid code that must decide, between everyone of your
ifstatements, whether or not to insert “and”. When you repeat the same logic over and over again you tend to multiply your errors and create a maintenance nightmare.)