Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
compiles perfectly but i just want to know how to make unwated coins to not appear.
ie. user input: 9.65
so output would be:
4 toonies 1 loony 2 quarters 1 dimes 1 nickel
buy mine shows this:
You entered the amount 9 dollars and 65 cents.
To make up this amount, use
4 toonies 1 loony 2 quarters 6 dime 13 nickels 15 pennies
End of processing
edited:
import javax.swing.JOptionPane; // Needed for JOptionPane
import java.util.Scanner;
/**
This program is for question 1.
need to use echo and make only needed coins appear and also space between lines
*/
package test;
public class coincounterr {
public static void main (String[] args){
//Declare variables
String input; //to ask user an amount of money
double money; //money user types in
int dollars;
double cents;
long t, l, q, d, n, p;
final int QUARTERS = 25;
final int DIME = 10;
final int NICKEL = 5;
final int PENNIES = 1;
//Ask user for an amount of money
input = JOptionPane.showInputDialog(null, "Enter any amount of money in $.");
money = Double.parseDouble(input);
//use println to display the end result
dollars = (int) money;
long cent = Math.round((money-Math.floor(money))*100);
System.out.println("You entered the amount " + dollars + " dollars and " + cent + " cents.\n");
t = dollars/2; //2 dollars
l = dollars%2; //1 dollar
q = cent/25; //25 cents
d = (cent%25)/10; //10 cents
n = (cent - (q*25)-(d*10))/5; //5 cents
p = cent - (q*25)-(d*10)-(n*5); //1cent
// output for toonies and loonies
System.out.println("To make up this amount, use \n");
if ( t<= 0)
System.out.println();
else if ( t > 1)
System.out.println(+t+" toonies \n");
else if ( t <= 1)
System.out.println(+t+" toony \n");
if (l<=0)
System.out.println();
else if ( l > 1)
System.out.println( + l + " loonies \n");
else if ( l <= 1)
System.out.println( +l+ " loony \n");
//use if statement for QUARTERS
if (q <= 0)
System.out.println();
else if (cent >= QUARTERS) //25 cents
System.out.println( +q+ " quarters \n");
if (d <= 0)
System.out.println();
else if ( cent >= DIME )
System.out.println ( +d+ " dime \n");
if (n <= 0)
System.out.println();
else if ( cent >= NICKEL)
System.out.println( +n+ " nickels\n");
if (p <= 0)
System.out.println();
else if ( cent >= PENNIES)
System.out.println( +p+ " pennies\n");
System.out.println("End of processing");
}
}
Add if (n > 0) before printing "unnecessary" coins. I won't post code so that you can keep practising ;)
EDIT: big hint:
else if (cent <= NICKEL) {
if (n > 0)
System.out.println(n+" nickels");
}
EDIT2: There is a problem in your computation: let's suppose you have 65 cents (cent = 65):
d = cent/10; // 6
n = cent/5; // 13, when what you want is probably 1
You're forgetting to substract the amount previously counted in d: if you have 65 cents and decided to get 65 / 10 = 6 coins of 10 cents, the remainder is 5 cents. You should work on the remainders at each step.
Good luck!
Related
First of all, I have found two other threads that have similar questions. There problem is that they didn't use the right equal sign for string and not formatting the if statements correctly for their particular problem.
For my assignment, I need to create a game called Pig where the player verses a computer in getting 100 points first in rolling pairs of dice. If the player rolls 1 on one turn, they get no additional points. If the player rolls two 1's, then they lose all their points. I have not coded the computer's turn yet, just focusing on the player. Please tell me what I am doing wrong. Thank you very much in advance.
import java.util.Scanner;
public class FourFive
{
public static void main (String[] args)
{
Pigs myopp = new Pigs();
Scanner scan = new Scanner (System.in);
final int Round = 20;
int num1, num2;
int roundTotal = 0;
int playerTotal = 0;
int compTotal = 0;
int win = 100;
int turnOver = 1;
Pigs die1 = new Pigs();
Pigs die2 = new Pigs();
String play = "y";
System.out.println("Type y to play");
play = scan.nextLine();
while (play.equalsIgnoreCase("y"))
{
for (int roll = 1; roll <= 6; roll++)//Each die has 6 sides
{
num1 = die1.roll();//rolls first die
num2 = die2.roll();//rolls second die
int points = num1 + num2;// adds dies up to get total for this turn
System.out.println("You roll " + num1 + " and " + num2);
if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points
points += 0;
else if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
playerTotal = 0;
else
System.out.println("you earned " + points + " this round");
playerTotal += points; total number of points per round added
System.out.println("You have a total of " + playerTotal);
System.out.println("Type y to play");
play = scan.nextLine();
}
}
}
}
My Output:
Type y to play
y
You roll 4 and 2
you earned 6 this round
You have a total of 6
Type y to play
y
You roll 6 and 5
you earned 11 this round
You have a total of 17
Type y to play
y
You roll 1 and 1
You have a total of 19 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 3
you earned 9 this round
You have a total of 28
Type y to play
y
You roll 1 and 1
You have a total of 30 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 4
you earned 10 this round
You have a total of 40
Type y to play
y
You roll 5 and 2
you earned 7 this round
You have a total of 47
Type y to play
y
You roll 5 and 1
You have a total of 53 //shouldn't add any additional points because of the "1"
Type y to play
If num1 is 1, then the first if condition takes it. It will not check the 'else if' condition. Similarly, if num2 is 1, the if condition takes it. So put your && condition first.
if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
playerTotal = 0;
else if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points
points += 0;
else
System.out.println("you earned " + points + " this round");
Your if logic is flawed and a bit redundant. Try this:
if (num1 == 1 && num2 == 1) {
playerTotal = 0;
}
else if (num1 != 1 && num2 != 1) {
playerTotal += points;
System.out.println("you earned " + points + " this round");
}
System.out.println("You have a total of " + playerTotal);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to complete an assignment, and I have almost fully completed it but I am have some trouble figuring out the last part.
How to: Check whether the number you received has five digits or not. Terminate gracefully if it doesn’t by notifying that the number should have 5 digits.
I've tried some stuff but I cannot get it to check correctly.
Here is my code:
import java.util.Scanner;
public class Five
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int number;
int digit1;
int digit2;
int digit3;
int digit4;
int digit5;
System.out.print( "Enter a five digit integer: " );
number = input.nextInt();
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 10000 % 1000 / 100;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;
System.out.printf( "Digits in %d are %d %d %d %d %d/n",
number, digit1, digit2, digit3, digit4, digit5 );
}
}
Thank you
You could ensure that the input is valid by doing a simple check of the user input like so
//take the user input as a string first
String userInput = input.next();
//then check the
if(userInput.length() == 5 && userInput.matches("\\d\\d\\d\\d\\d"))
{
number = Integer.parseInt(userInput);
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 10000 % 1000 / 100;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;
System.out.printf( "Digits in %d are %d %d %d %d %d/n",
number, digit1, digit2, digit3, digit4, digit5 );
}
else
{
System.out.println(userInput + " is not a valid 5 digit number");
}
By all means, keep your current code and only submit something you wrote and feel comfortable with.
Using regex
I don't know if you know regular expression or if you are even allowed to use them. You could do something like this:
String userInput = input.next().trim();
if (userInput.matches("\\d{5}")) {
String[] split = userInput.split("(?<=[0-9])");
System.out.printf("Digits in %s are", userInput);
for (int i = 0; i < split.length; i++) {
System.out.printf(" %s", split[i]);
}
}
Input with Leading zeros
Only thing I am unsure about your requirements is leading zeros (and perhaps you need to think about that for your own solution too).
Is 01234 a valid 5 digits input ?
Ignoring leading zeros
If leading zeros are to be ignored, then reading an int and converting it to String will remove the leading zeros.
Scanner input = new Scanner(System.in);
int number = input.nextInt();
String userInput = String.valueOf(number);
if (userInput.length() == 5) {
String[] split = userInput.split("(?<=[0-9])");
System.out.printf("Digits in %s are", userInput);
for (int i = 0; i < split.length; i++) {
System.out.printf(" %s", split[i]);
}
}
I am having a hard time understanding how to convert amounts such as $1.17 into this output:
1 dollar,
1 dime,
1 nickel,
2 pennies.
I need to use if statements which I can figure out but, the problem i am having is trying to get the change amounts to display correctly. Here is my code. I`m a visual learner so if you start me in the right direction that would be helpful.
import java.util.Scanner;
public class ComputeChange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double, for example 11.56: " );
double number = input.nextDouble();
System.out.println("Your amount " + number + " consists of ");
int Dollars = (int) (number);
int Quarters = Dollars / 25;
if (number == 1) {
System.out.print("1 dollar ");
}
else if (number > 1) {
System.out.print((int)Dollars + " dollars ");
}
if (number == 0) {
System.out.println("");
}
System.out.print( Quarters + " Quarters ");
}
}
You will probably want to use the modulus operator %. It is used with 2 numbers and returns the remainder of a divided by b where a is the left hand assignment and b is the right hand represented like a%b.
Example:
11%2=1 explanation: 5*2 = 10, 11-10 = 1
.66%.25=.16 explanation: 2*.25 = .5, .66-.5=.16
Start with a simple problem where you only have 2 types of coins, say 8¢ or $0.08.
double monies = .08;
int numNickles = (int)(monies/.05) = 1 // one nickle
monies = monies % .05; // or you can write as monies %=.05;
// monies value should now be .03
int numPennies = (int)(monies/.01) = 3 // three pennies
A much simpler approach than using modulus is to calculate the units of measurement in top-down fashion (from the highest to the lowest) and keep deducting them from the total already converted into the lowest unit.
A lot of libraries use this approach with units of time as well i.e. converting a time span into hours, minutes and seconds. Here's the same approach for currency. I've added inline comments to explain the code as best as possible.
// Scan the amount
Scanner scanner = new Scanner(System.in);
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.close();
// convert into cents
int cents = (int) (amount * 100);
// get dollars
int dollars = cents/100;
// cents left after dollars
cents = cents - dollars*100;
// get quarters
int quaters = cents/25;
// cents left after quarters
cents = cents - quaters*25;
// get dimes
int dimes = cents/10;
// cents left after dimes
cents = cents - dimes*10;
// get nickels
int nickels = cents/5;
// cents left after nickels
cents = cents - nickels*5;
// leftover cents are pennies
int pennies = cents;
Now, just build the output message with a StringBuilder as
StringBuilder msg = new StringBuilder("You have:");
if (dollars > 0) {
msg.append(" ").append(dollars).append(" dollar").append(dollars > 1 ? "s" : "");
}
if (quaters > 0) {
msg.append(" ").append(quaters).append(" quarter").append(quaters > 1 ? "s" : "");
}
if (dimes > 0) {
msg.append(" ").append(dimes).append(" dime").append(dimes > 1 ? "s" : "");
}
if (nickels > 0) {
msg.append(" ").append(nickels).append(" nickel").append(nickels > 1 ? "s" : "");
}
if (pennies > 0) {
msg.append(" ").append(pennies).append(" pennie").append(pennies > 1 ? "s" : "");
}
System.out.println(msg);
Output :
Enter amount: 1.17
You have: 1 dollar 1 dime 1 nickel 2 pennies
Enter amount: 12.99
You have: 12 dollars 3 quarters 2 dimes 4 pennies
I got the dollars to work but now I can`t figure out how to display the quarters, dimes, nickels, and pennies correctly. Technically they show up but my professor wants $1.35 to show up as 1 dollar 1 quarter and 1 dime. But, mine shows up as 1 dollars 5 Quarters 13 Dimes 27 Nickels 135 Pennies. Here is my code:
import java.util.Scanner;
public class ComputeChange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double, for example 11.56: " );
double number = input.nextDouble();
System.out.println("Your amount " + number + " consists of ");
int remainingamount = (int)(number*100);
int Quarters = remainingamount/25;
int Dimes = remainingamount/10;
int Nickels = remainingamount/5;
int Pennies = remainingamount;
if (number == 1) {
System.out.print("1 dollar ");
}
else if (number > 1) {
System.out.print((int)number + " dollars ");
}
if (number == 0) {
System.out.println("");
}
System.out.print(Quarters + " Quarters " + Dimes + " Dimes " + Nickels + " Nickels " + Pennies + " Pennies");
}
}
For each different coin you calculate, you need to remove this from the remaining amount. Such as (untested):
int Dollars = (int)number;
int remainingamount = (int)((number-Dollars)*100);
int Quarters = remainingamount/25;
remainingamount -= Quaters * 25;
int Dimes = remainingamount/10;
remainingamount -= Dimes * 10
int Nickels = remainingamount/5;
remainingamount -= Nickels * 5
int Pennies = remainingamount;
Since you're using Java, have you tried JSR 354?
See JavaMoney.org or the JSR 354 Detail page at JCP.org It offers standard API for the conversion of monetary units and default exchange rate providers by the IMF or European Central Bank.
I'm working on a program that will calculate the basic interest accrued on a certificate of deposit. The program asks for the amount of money invested and the term (up to five years). Depending on how many years their term is, is what determines how much interest is earned. I use an if/else statement to determine the rate of interest. I then use a loop to print out how much money is in the account at the end of each year. My problem is that when I run the program, the money is not counting.
Here is the entire code.
import java.util.Scanner;
public class CDCalc
{
public static void main(String args[])
{
int Count = 0;
double Rate = 0;
double Total = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("How much money do you want to invest?");
int Invest = userInput.nextInt();
System.out.println("How many years will your term be?");
int Term = userInput.nextInt();
System.out.println("Investing: " + Invest);
System.out.println(" Term: " + Term);
if (Term <= 1)
{
Rate = .3;
}
else if (Term <= 2)
{
Rate = .45;
}
else if (Term <= 3)
{
Rate = .95;
}
else if (Term <= 4)
{
Rate = 1.5;
}
else if (Term <= 5)
{
Rate = 1.8;
}
int count = 1;
while(count <= 5)
{
Total = Invest + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}
}
}
and here is the result I get with a 10 dollar investment, just to keep it simple, and a 5 year investment.
How much money do you want to invest?
10
How many years will your term be?
5
Investing: 10
Term: 5
Value after year 1: 10.18
Value after year 2: 10.18
Value after year 3: 10.18
Value after year 4: 10.18
Value after year 5: 10.18
My main problem is I dont know how to make it continually add the intrest onto the total. I'm not sure if I need to use a different loop or what. Any help would be appreciated.
Total = Invest + (Invest * (Rate) / (100.0));
You are not changing the value of Invest for each year, so it is not compounding. It is like you are getting .18$ of interest each year, retired from the account.
Change Total for Invest.
You need to add the investment interest to your total:
Total = Invest;
int count = 1;
while(count <= 5)
{
Total = Total + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}