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:
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