sdken8
Oct 1, 2012, 09:33 AM
Hello,
I'm making a simple currency java program, and for the most part, it works well. What it is, is the user inputs the number of coin types, and the program will output the total in $x.xx format. My problem is that if the pennies total less than 10, it will display incorrectly. For example try typing 4 quarters, 0 dimes, 0 nickels and 1 penny. Here is the code:
import java.util.Scanner;
public class ValueOfCoins {
public ValueOfCoins() {
}
public static void main(String[] args)
{
int quarters;
int dimes;
int nickels;
int pennies;
int total;
Scanner scanner = new Scanner (System.in);
System.out.print("Enter the number of quarters: ");
quarters = scanner.nextInt();
System.out.print("Enter the number of dimes: ");
dimes = scanner.nextInt();
System.out.print("Enter the number of nickels: ");
nickels = scanner.nextInt();
System.out.print("Enter the number of pennies: ");
pennies = scanner.nextInt();
int totalInCents = quarters * 25 + dimes * 10 + nickels * 5 + pennies;
total = totalInCents / 100;
totalInCents = totalInCents % 100;
System.out.println("The value of these coins is $" + total + "." + totalInCents);
}
}
Thanks in advance!
-Ksd
I'm making a simple currency java program, and for the most part, it works well. What it is, is the user inputs the number of coin types, and the program will output the total in $x.xx format. My problem is that if the pennies total less than 10, it will display incorrectly. For example try typing 4 quarters, 0 dimes, 0 nickels and 1 penny. Here is the code:
import java.util.Scanner;
public class ValueOfCoins {
public ValueOfCoins() {
}
public static void main(String[] args)
{
int quarters;
int dimes;
int nickels;
int pennies;
int total;
Scanner scanner = new Scanner (System.in);
System.out.print("Enter the number of quarters: ");
quarters = scanner.nextInt();
System.out.print("Enter the number of dimes: ");
dimes = scanner.nextInt();
System.out.print("Enter the number of nickels: ");
nickels = scanner.nextInt();
System.out.print("Enter the number of pennies: ");
pennies = scanner.nextInt();
int totalInCents = quarters * 25 + dimes * 10 + nickels * 5 + pennies;
total = totalInCents / 100;
totalInCents = totalInCents % 100;
System.out.println("The value of these coins is $" + total + "." + totalInCents);
}
}
Thanks in advance!
-Ksd