program : giving back change with dollars, dimes and pennies
When I run it , it wont print out the nickels and pennies.
I was asked to only put the input in pennies...
Code java:
package givingChange;
import javax.swing.JOptionPane;
public class givingChange {
public static void main(String[] args) {
String amount;
int price, paid, change;
int dollars, quarters, dimes, pennies, nickels;
// In pennies
amount=
JOptionPane.showInputDialog("Enter amount due");
price = Integer.parseInt(amount);
amount=
JOptionPane.showInputDialog("Enter amount paid");
paid = Integer.parseInt(amount);
change = paid - price;
dollars = change/100;
int left= change%100;
quarters = left/25;
left= left%25;
dimes = left/10;
left=left%10;
nickels = left/5;
left = left%5;
pennies = left/1;
if (dollars>0)
{ System.out.println(dollars+" dollars"); }
if (quarters>0)
{System.out.println(quarters+ " quarters");}
if (dimes>0)
{System.out.println(dimes+" dimes");
if (nickels>0)
{System.out.println(nickels+ " nickels");}
if (pennies>0)
{System.out.println(pennies+" pennies");}
}
}
}
Re: program : giving back change with dollars, dimes and pennies
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Quote:
it wont print out the nickels and pennies
Their values must be 0. Check your code and logic to be sure your formulas are correct.
Try debugging the code by printing out the values of the variables as they are changed. For example print out the value of left after every time it is assigned a new value. The print out will show you where your formulas are going wrong.
Re: program : giving back change with dollars, dimes and pennies
Quote:
Originally Posted by
hellynam
When I run it , it wont print out the nickels and pennies
Maybe if you adopt a style with consistent indentation to emphasize control structures you might avoid certain problems like this.
I mean, the compiler ignores these stylistic niceties, but for humans, there is a reason that experienced programmers often get in the habit of making their code look "neat" in a more-or-less standard form.
- Each block should start and end in the same column.
- Inner Block contents are indented more than outer blocks.
- For emphasis, sometimes people put comments at the closing '}' of some of the blocks.
- Judicious (and consistent) use of whitespace sometimes makes things more readable or at least more attractive...
One style that seems to be popular with Java programmers might make your code look like the following:
Code java:
package givingChange;
import javax.swing.JOptionPane;
public class GivingChange {
public static void main(String [] args) {
String amount;
int price, paid, change;
int dollars, quarters, dimes, pennies, nickels;
amount = JOptionPane.showInputDialog("Enter amount due");
price = Integer.parseInt(amount);
amount = JOptionPane.showInputDialog("Enter amount paid");
paid = Integer.parseInt(amount);
change = paid - price;
dollars = change / 100;
int left = change % 100;
quarters = left / 25;
left = left % 25;
dimes = left / 10;
left = left % 10;
nickels = left / 5;
left = left % 5;
pennies = left / 1;
if (dollars > 0) {
System.out.println(dollars + " dollars");
} // End of "if (dollars...)"
if (quarters > 0) {
System.out.println(quarters + " quarters");
} // End of "if (quarters...)"
if (dimes > 0) {
System.out.println(dimes + " dimes");
if (nickels > 0) {
System.out.println(nickels + " nickels");
}
if (pennies > 0) {
System.out.println(pennies + " pennies");
}
} // End of "if (dimes...)"
} // End of main()
} // End of class definition
Cheers!
Z