| | |
simple coin program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
i have to write a simple java program
write an application that determines the value of coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels and pennies
so i did it i don't know where to go from here help would be appreciated
public class coins {
public static void main(String[] args) {
double quarters;
double dimes;
double nickels;
double pennies;
double money;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of quarters in the jar.");
quarters = scan.nextInt();
System.out.println("Enter the number of dimes in the jar");
dimes= scan.nextInt();
System.out.println("Enter the number of nickels in the jar");
nickels = scan.nextInt();
System.out.println("Enter the number of pennies in the jar");
pennies = scan.nextInt();
money = quarters*25 + dimes*10 + nickels*5 + pennies*1;
System.out.println("Your total change in dollars and cents is "+money);
}
}
the last 2 have to be in dollar like 81 dollars and 27 cents but i get dollar is and cents is 81.00 i dont no the code to split it,i dont understand floating what where do i put it ??? any one help me out
write an application that determines the value of coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels and pennies
so i did it i don't know where to go from here help would be appreciated
public class coins {
public static void main(String[] args) {
double quarters;
double dimes;
double nickels;
double pennies;
double money;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of quarters in the jar.");
quarters = scan.nextInt();
System.out.println("Enter the number of dimes in the jar");
dimes= scan.nextInt();
System.out.println("Enter the number of nickels in the jar");
nickels = scan.nextInt();
System.out.println("Enter the number of pennies in the jar");
pennies = scan.nextInt();
money = quarters*25 + dimes*10 + nickels*5 + pennies*1;
System.out.println("Your total change in dollars and cents is "+money);
}
}
the last 2 have to be in dollar like 81 dollars and 27 cents but i get dollar is and cents is 81.00 i dont no the code to split it,i dont understand floating what where do i put it ??? any one help me out
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
i have to write a simple java program
write an application that determines the value of coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels and pennies
so i did it i don't know where to go from here help would be appreciated
public class coins {
public static void main(String[] args) {
double quarters;
double dimes;
double nickels;
double pennies;
double money;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of quarters in the jar.");
quarters = scan.nextInt();
System.out.println("Enter the number of dimes in the jar");
dimes= scan.nextInt();
System.out.println("Enter the number of nickels in the jar");
nickels = scan.nextInt();
System.out.println("Enter the number of pennies in the jar");
pennies = scan.nextInt();
money = quarters*25 + dimes*10 + nickels*5 + pennies*1;
System.out.println("Your total change in dollars and cents is "+money);
}
}
the last 2 have to be in dollar like 81 dollars and 27 cents but i get dollar is and cents is 81.00 i dont no the code to split it,i dont understand floating what where do i put it ??? any one help me out
Well, you can use DecimalFormat to format
money after dividing by 100. quarters , dimes , nickels , pennies should be integers, not doubles. •
•
•
•
quarters = scan.nextInt();
or you can make
money an integer and have two variables, dollars and cents, and split money up into those components using the / and % operators. So if money = 2343, dollars would equal 23 and cents would equal 43. Then output a dollar sign, then dollars , then a decimal point, then cents . You'd have to pad cents with a 0 if it's less than ten cents. ![]() |
Similar Threads
- Projects for the Beginner (Python)
- c+ Coin tossing program (C)
- simple cash register (pos) problem - please help!! (VB.NET)
- New Python Problem (Python)
Other Threads in the Java Forum
- Previous Thread: how will be hostname in dns of a normal client??
- Next Thread: Having trouble with loops
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






