Problem in Coin-counter with scanner class
Hey all, am currently working on a small project, where the user enters an amount in cents and the program will input the least amount of coins needed (2 dollars, 1 dollars, 50 cents, 20 cents, 10 cents, 5 cents and 1 cents).
Example Output...
Please Enter amount in cents: 477
Least number of coins needed is as follows
2 coins – 2
1 coins – 0
50 cents coins – 1
20 cents coins – 1
10 cents coins – 0
5 cents coin – 1
1 cent coins – 2
Total coins needed 7.
So far i have done:
Code :
import java.util.*
public class CoinCounter {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
int amount;
int oneDollar
int twoDollar;
int fiftyCents;
int twentyCents;
int tenCents;
int fiveCents;
int oneCents;
int totalCoins = 0;
System.out.println("Enter amount in cents: ")
amount=sc.nextInt();
System.out.println((amount / 200) + " – $2 coins");
amount = amount % 200;
System.out.println((amount / 100) + " – $1 coins");
amount = amount % 100;
System.out.println((amount / 50) + " – $0.50 coins");
amount = amount % 50;
System.out.println((amount / 20) + " – $0.20 coins");
amount = amount % 20;
System.out.println((amount / 10) + " – $0.10 coins");
amount = amount % 10;
System.out.println((amount / 5) + " – $0.05 coins");
amount = amount % 5;
System.out.println(amount + " – $0.01 coins");
}
}
Im not sure if that is correct, but im now stuck on what i should do next?
Re: CoinCounter with scanner class.
Hello coccoster and welcome to the Java programming forums :)
There are some missing semicolons ( ; ) in your code above.
Have you been able to compile it?
Your code seems to be working fine once the semicolons are in place.
Re: CoinCounter with scanner class.
Hey coccoster,
I have just added the functionality to allow you to display the number of coins needed in your code.
Code :
import java.util.*;
public class CoinCounter {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
int amount;
int oneDollar;
int twoDollar;
int fiftyCents;
int twentyCents;
int tenCents;
int fiveCents;
int oneCents;
int totalCoins = 0;
int myInt;
System.out.println("Enter amount in cents: ");
amount=sc.nextInt();
System.out.println((amount / 200) + " – $2 coins");
myInt = (amount / 200);
amount = amount % 200;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 100) + " – $1 coins");
myInt = (amount / 100);
amount = amount % 100;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 50) + " – $0.50 coins");
myInt = (amount / 50);
amount = amount % 50;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 20) + " – $0.20 coins");
myInt = (amount / 20);
amount = amount % 20;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 10) + " – $0.10 coins");
myInt = (amount / 10);
amount = amount % 10;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 5) + " – $0.05 coins");
myInt = (amount / 5);
amount = amount % 5;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println(amount + " – $0.01 coins");
myInt = (amount);
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println("Total Coins: " + totalCoins);
}
}
Code :
Enter amount in cents:
477
2 – $2 coins
0 – $1 coins
1 – $0.50 coins
1 – $0.20 coins
0 – $0.10 coins
1 – $0.05 coins
2 – $0.01 coins
Total Coins: 7
Hope this all works as expected :D
Re: CoinCounter with scanner class.
Quote:
Originally Posted by
JavaPF
Hey coccoster,
I have just added the functionality to allow you to display the number of coins needed in your code.
Code :
import java.util.*;
public class CoinCounter {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
int amount;
int oneDollar;
int twoDollar;
int fiftyCents;
int twentyCents;
int tenCents;
int fiveCents;
int oneCents;
int totalCoins = 0;
int myInt;
System.out.println("Enter amount in cents: ");
amount=sc.nextInt();
System.out.println((amount / 200) + " – $2 coins");
myInt = (amount / 200);
amount = amount % 200;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 100) + " – $1 coins");
myInt = (amount / 100);
amount = amount % 100;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 50) + " – $0.50 coins");
myInt = (amount / 50);
amount = amount % 50;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 20) + " – $0.20 coins");
myInt = (amount / 20);
amount = amount % 20;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 10) + " – $0.10 coins");
myInt = (amount / 10);
amount = amount % 10;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println((amount / 5) + " – $0.05 coins");
myInt = (amount / 5);
amount = amount % 5;
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println(amount + " – $0.01 coins");
myInt = (amount);
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
System.out.println("Total Coins: " + totalCoins);
}
}
Code :
Enter amount in cents:
477
2 – $2 coins
0 – $1 coins
1 – $0.50 coins
1 – $0.20 coins
0 – $0.10 coins
1 – $0.05 coins
2 – $0.01 coins
Total Coins: 7
Hope this all works as expected :D
Thanks mate your a champion.. Would you mind explaining to me why you chose myInt as the variable name?
and could you also explain to me the for loops you have used?
Re: CoinCounter with scanner class.
Quote:
Originally Posted by coccoster
Thanks mate your a champion.. Would you mind explaining to me why you chose myInt as the variable name?
and could you also explain to me the for loops you have used?
Glad I could help!
There was no real reason why I named that integer variable 'myInt'. You can call a variable pretty much what ever you want. I normally try to make the name descriptive so I can easily see what its used for but in this case I didn't put too much thought into it.
This bit of code is used for counting each coin used:
Code :
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
It first checks to see if the 'amount' is greater than 0. If it is, that means a coin is being used so we need to count it. If not then it skips this part.
myInt = (amount / 100);
gives us the number of coins used. For example, 2.
The for loop executes from 0 to the amount of coins used (myInt) so if there are 2 coins, then totalCoins++ is executed twice, adding 2 to the totalCoins value.
Re: CoinCounter with scanner class.
Quote:
Originally Posted by
JavaPF
Glad I could help!
There was no real reason why I named that integer variable 'myInt'. You can call a variable pretty much what ever you want. I normally try to make the name descriptive so I can easily see what its used for but in this case I didn't put too much thought into it.
This bit of code is used for counting each coin used:
Code :
if(amount > 0){
for(int a = 0; a < myInt; a++ ){
totalCoins++;
}
}
It first checks to see if the 'amount' is greater than 0. If it is, that means a coin is being used so we need to count it. If not then it skips this part.
myInt = (amount / 100);
gives us the number of coins used. For example, 2.
The for loop executes from 0 to the amount of coins used (myInt) so if there are 2 coins, then totalCoins++ is executed twice, adding 2 to the totalCoins value.
Thanks you helped alot mate.
Re: CoinCounter with scanner class.
Always glad to help! Good luck with the rest of your project.