Change Machine Only Counting Quarters - java

I'm having an issue with a change machine I'm building for my Java class. I'm very new to programming so this may be a dumb logic error. My change machine asks the user to input the price of the item then amount paid then it is supposed to calculate how much change the user will receive in quarters, dimes, etc... However, it's only counting 1 quarter each time. Here's the code:
import java.util.Scanner;
import java.text.*;
public class Main {
public static void main(String[] args)
{
float penny = .1F, nickel = .5F, dime = .10F, quarter = .25F;
int pennyCtr = 0, nickelCtr = 0, dimeCtr = 0, quarterCtr = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter Purchase Price: ");
float price = scan.nextFloat();
DecimalFormat paidFormat = new DecimalFormat("0.00");
System.out.println("Enter Amount Paid: ");
float paid = scan.nextFloat();
float change = (float) (paid - price);
System.out.println("Your change from $" + paidFormat.format(paid)
+ " is: " + change);
if (change >= .25)
{
change -= quarter;
quarterCtr++;
}
else if (change < .25)
{
change -= dime;
dimeCtr++;
}
else if (change < .10)
{
change -= nickel;
nickelCtr++;
}
else if (change < .5)
{
change -= penny;
pennyCtr++;
}
System.out.println("Your change from $" + paidFormat.format(paid) +
" is: " + quarterCtr + " Quarters, " + dimeCtr + " Dimes, "
+ nickelCtr + " Nickles, " + pennyCtr + " Pennies. ");
System.out.println("Program written by Ashley ");
}
}

Some general hints:
Look at how you are declaring your variables, especially penny and nickel.
Look at how you are calculating change. Is that right?
You need a while loop. How long should it loop for? When should the loop end?
When you print out "Your change from...", consider how you could output the change neatly.
Google some questions about float subtraction - it's not as easy as it first seems! Consider using double instead for your declarations and input.

A quick look at the code tells me that you are missing a loop between this block of code:
if (change >= .25)
{
change -= quarter;
quarterCtr++;
}
else if (change < .25)
{
change -= dime;
dimeCtr++;
}
else if (change < .10)
{
change -= nickel;
nickelCtr++;
}
else if (change < .5)
{
change -= penny;
pennyCtr++;
}
Essentially, your program will terminate before it can reduce the change to 0. This is why you are only counting 1 coin every time. Consider maybe a while loop (while change > 0) perhaps.

Related

Why am I getting an error message while trying to code for change?

This is the question: A store clerk wants a program that calculates the exact change to give a customer with the minimum number of bills and coins. You should input the amount of the purchase and the amount of money tendered (given to the clerk) and then output the change required and the number of each bill and coin to make up the change. Remember you want the minimum number of coins and bills in each case.
** Plan out this program first, before writing any code **
A sample run of the program is shown below.
Change Making Program
Please enter the total purchase: $1.42
Please enter the amount tendered: $20.00
The change will be: $18.58
To make up this amount you will need:
1 ten-dollar bill
1 five-dollar bill
1 two-dollar coin
1 loonie
2 quarters
1 nickel
3 pennies
Thank you for using the Change Making Program
This is what I have so far:
package defaultpackage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ChangeMaker {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the total of your purchase: $");
double total = Integer.parseInt(stdin.readLine());
System.out.print("Please enter the amount of money tendered: $");
double tendered = Integer.parseInt(stdin.readLine());
double change = tendered - total;
System.out.print("Your change is: $" +change);
if (change > 99.99)
{
double hundreds = (change - (change % 100)) / 100;
System.out.print("Hundred-dollar bills: " + hundreds);
change = change - (hundreds * 100);
}
if (change > 49.99)
{
double fifties = (change - (change % 50)) / 50;
System.out.print("Fifty-dollar bills: " + fifties);
change = change - (fifties * 50);
}
if (change > 19.99)
{
double twenties = (change - (change % 20)) / 20;
System.out.print("Twenty-dollar bills: " + twenties);
change = change - (twenties * 20);
}
if (change > 9.99)
{
double tens = (change - (change % 10)) / 10;
System.out.print("Ten-dollar bills: " + tens);
change = change - (tens * 10);
}
if (change > 4.99)
{
double fives = (change - (change % 5)) / 5;
System.out.print("Five-dollar bills: " + fives);
change = change - (fives * 5);
}
if (change > 1.99)
{
double toonies = (change - (change % 2)) / 2;
System.out.print("Toonies: " + toonies);
change = change - (toonies * 2);
}
if (change > 0.99)
{
double loonies = (change - (change % 1)) / 1;
System.out.print("Loonies: " + loonies);
change = change - (loonies * 1);
}
if (change > 0.24)
{
double quarters = (change - (change % 0.25)) / 0.25;
System.out.print("Quarters: " + quarters);
change = change - (quarters * 0.25);
}
if (change > 0.09)
{
double dimes = (change - (change % 0.1)) / 0.1;
System.out.print("Dimes: " + dimes);
change = change - (dimes * 0.1);
}
if (change > 0.04)
{
double nickles = (change - (change % 0.05)) / 0.05;
System.out.print("Nickles: " + nickles);
change = change - (nickles * 0.05);
}
if (change > 0)
{
double pennies = (change - (change % 0.1)) / 0.1;
System.out.print("Pennies : " + pennies);
change = change - (pennies * 0.1);
}
}
}
Here is a solution to the problem you present. It produces exactly the desired output.
The code uses integer math to avoid the precision problems you will inevitably have when using floating point values to represent dollar amounts. To do this, input values are multiplied by 100 and treated as cents. When printing out dollar amounts, we divide by 100 to represent the values in dollars.
Another feature of this code is that it is data-driven. Rather than duplicate the logic for removing each denomination over and over, a table provides the information about each denomination, and then there is only one block of code that is run in a loop to deal with each denomination. The code deals with properly pluralizing each denomination name when appropriate, including the special case for "pennies".
package defaultpackage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
class Denomination {
public Denomination(int value, String name, String pluralName) {
this.value = value;
this.name = name;
this.pluralName = pluralName;
}
public Denomination(int value, String name) {
this(value, name, null);
}
String getDescription(int count) {
String desc = this.name;
if (count > 1)
if (pluralName == null)
desc += 's';
else
desc = pluralName;
return desc;
}
public int getValue() {
return value;
}
private final int value;
private final String name;
private final String pluralName;
}
class ChangeMaker {
private static final List<Denomination> denominations = List.of(
new Denomination(10000, "one-hundred-dollar bill"),
new Denomination(5000, "fifty-dollar bill"),
new Denomination(2000, "twenty-dollar bill"),
new Denomination(1000, "ten-dollar bill"),
new Denomination(500, "five-dollar bill"),
new Denomination(200, "two-dollar coin"),
new Denomination(100, "loonie"),
new Denomination(25, "quarter"),
new Denomination(10, "dime"),
new Denomination(5, "nickel"),
new Denomination(1, "penny", "pennies")
);
public static void main(String[] args) throws IOException {
System.out.println("Change Making Program");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the total purchase: $");
int total = (int)(Double.parseDouble(stdin.readLine()) * 100);
System.out.print("Please enter the amount tendered: $");
int tendered = (int)(Double.parseDouble(stdin.readLine()) * 100);
int change = tendered - total;
System.out.printf("The change will be: $%.2f\n", change / 100.0);
System.out.println("To make up this amount you will need:");
for (Denomination denom: denominations) {
if (change == 0)
break;
if (change >= denom.getValue()) {
int count = change / denom.getValue();
if (count > 0) {
change = change - denom.getValue() * count;
System.out.printf("%d %s\n", count, denom.getDescription(count));
}
}
}
System.out.println("Thank you for using the Change Making Program");
}
}
Sample Run:
Change Making Program
Please enter the total purchase: $1.42
Please enter the amount tendered: $20.00
The change will be: $18.58
To make up this amount you will need:
1 ten-dollar bill
1 five-dollar bill
1 two-dollar coin
1 loonie
2 quarters
1 nickel
3 pennies
Thank you for using the Change Making Program
If the OP is asking this question to do a homework assignment, then I expect that this answer may be "too good". It may be using techniques not yet studied in class. Even if the OP could submit this as-is, I would encourage them to not do so. Instead, I'd suggest that they study this example, learn from it, and then incorporate what is learned into their own solution as appropriate. I encourage them to ask questions about the example, which I'd be happy to answer.
I have overseen the parseInt problem, but there is still another problem:
pennies = (change - (change % 0.1)) / 0.1;
...
change = change - (pennies * 0.1);
should be 0.01 instead of 0.1 - or just use what is left in change.
Advice: do not use double for money - lots of problems with rounding! (chances of change being short by one penny)
either use int/ long to represent cents, or
use BigDecimal
Note (change - (change % 100)) / 100 is basically the same as (int)(change / 100) - casting to int removes all decimals

Variable may not have been initialized, does not work when i do initialize

I'm trying to write a program that outputs the change of a payment in dollars, quarters, dimes, etc. I declare all the coin variables at the beginning of the program, but don't initialize them, and I get this error when I try to initialize them later in the program. So, I just initialized them to 0 to initialize them and then it doesn't recognize that I'm changing the values and they stay at 0.
import java.util.Scanner;
public class ChangeClient {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickles = 0;
int pennies = 0;
double change = 0.0;
double newChange = 0.0;
System.out.println("How much was it? ");
double cost = kb.nextDouble();
System.out.println("What did you pay with? ");
double payment = kb.nextDouble();
if(payment-cost == 0) {
change = 0.0;
}
else {
change = payment - cost; //4.73
newChange = change;
if((newChange-change != 0 && newChange >= 1)) {
dollars = (int)(change/1);//dollars = 4
newChange = (change - dollars);// newchange = .73
}
if((newChange - change) != 0 && newChange >= .25) {
quarters = (int)(newChange/.25); //quarters = 2
newChange = (change - (dollars + (quarters*.25))); //newchange = 4.73 - (4+ (2*.25)) = .23
}
if(newChange - change != 0 && newChange >= .10){
dimes = (int)(newChange/.10); //dimes = 2
newChange = (change -(dollars + (quarters * .25) + (dimes * .10))); //newchange = 4.73 - (4+ (2*.25) + (2*.10)) = .03
}
if(newChange - change != 0 && newChange >= .05) {
nickles = (int)(newChange/.05);
newChange = (change -(dollars + (quarters * .25) + (dimes * .10) + (nickles * .05))); //newchange is less than .05 so skip
}
if(newChange - change != 0 && newChange >= .01) {
pennies = (int)(newChange/.01); //pennies = 3
newChange = (change -(dollars + (quarters * .25) + (dimes * .10) + (nickles * .05) + (pennies * .01))); //newchange (4.73 - (4 + (2*.25) + (2 .10) + (3*.03))) = 0
}
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes " + dimes);
System.out.println("Nickles " + nickles);
System.out.println("Pennies " + pennies);
System.out.println("Total change due: " + change);
}
}
}
I know there are a ton of people who get errors like this and I've looked through all of them to find a solution, but none of them were the same as the problem I'm having.
I think the problem is this here:
` newChange = change;
if((newChange-change != 0 && newChange >= 1)) `
if you set newChange to change, then newChange-change will always be 0.
So you all of your if-statements will evaluate to false.
For the precision problem:
An easy solution would be to multiply the inputs with 100 and work with integers. Just think of handling cents instead of Dollars ;-)
Your problem is with this assignment:
newChange = change;
Followed these comparisons:
if ((newChange - change) != 0 && newChange >= .25)
if (newChange - change != 0 && newChange >= .10)
etc...
You initialize newChange = change, so newChange - change will always equal zero and all of your if blocks will be skipped.
A few more notes:
-Consider using the debug mode of your IDE. You can set a breakpoint and see the values of variables as your program executes each line. Java has fantastic debugging (including remote debugging). Here's how your program looks in my IDE (Eclipse Neon):
-A full-featured Java IDE (the big two are Eclipse and IntelliJ, though NetBeans has at least one devotee at my office) leverages many of the features of compile-time type-safety, debugging, and Java conventions. You can rename a variable or method throughout an application with a single operation, you can see whether a method you are trying to call exists, you can even call a method that doesn't exist yet and let the IDE stub it out for you. All these are possible because the IDE is constantly compiling the code in the background as you write it, so it knows every place a class, variable, or method is used. Debugging allowed me to find the error in your code in (I kid you not) 10 seconds. This is why we write Java for enterprise-grade applications!
-Consider using the objects Double and Integer instead of instead of the primitive types double and int, and use the comparison .equals(). In the object-oriented world, we prefer to abstract out the implementation of a comparison from the comparison itself. Consider this example:
Double first = 3.5;
Double second = 3.5;
boolean equal = (first == second); // false
In this example, Java compares the pointers to first and second, which point to different locations in memory (because each variable has its own space allocated in memory and first and second are just references to those locations).
This refactor will have the expected behavior in all cases:
Double first = 3.5;
Double second = 3.5;
boolean equal = first.equals(second); // true
The authors of the Double class have implemented a .equals() method that ensures the comparison is of the values, not the references. This is another strength of OO programming; you can #Override a default .equals() method and implement your own based on whatever criteria you want. This is useful if, for example, you want two class instances to be considered equal based on just one or two of their fields. In your case, using double primitives, it's certainly not critical to use .equals(), but it is a good habit to get into or you will run into surprises with the String class and others.
Good luck!
I've corrected your code.
import java.util.Scanner;
public class ChangeClient {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int dollars;
int quarters;
int dimes;
int nickles;
int pennies;
double change;
double newChange;//no need of newChange
System.out.println("How much was it? ");
double cost = kb.nextDouble();
System.out.println("What did you pay with? ");
double payment = kb.nextDouble();
if(payment-cost == 0) {
change = 0.0;
}
else {
change = payment - cost; //4.73
if(change >= 1) {
dollars = (int)(change/1);//dollars = 4
change = (change - dollars);// change = .73
System.out.println("Dollars: " + dollars);
}
if(change >= .25) {
quarters = (int)(change/.25); //quarters = 2
change = (change - quarters*.25); //change = .73-2*.25 = .23
System.out.println("Quarters: " + quarters);
}
if(change >= .10){
dimes = (int)(change/.10); //dimes = 2
change = (change -dimes * .10); //change = .23-2*.10 = .03
System.out.println("Dimes " + dimes);
}
if(change >= .05) {
nickles = (int)(change/.05);
change = (change -nickles * .05); //change is less than .05 so skip
System.out.println("Nickles " + nickles);
}
if(change >= .01) {
pennies = (int)(change/.01); //pennies = 3
change = (int)(change -pennies * .01); //change = .03 - 3*.01 = 0
System.out.println("Pennies " + pennies);
System.out.println("Total change due: " + change);
}
}
}
}
newChange which you have used is wrong as pointed out by others and you wont even need it. If you go through my corrections, I've used change itself to be used throughout. Hope this helps.

Doing a change program for my java class, not sure how to loop the whole program?

Currently I am working on a program in which the user enters a value in cents (so $1.25 would be 125), and the program determines how many coins are given in the fewest amount. I have this working.
What is troubling me is that my professor wants the program to continuously loop until the user enters a value less than 0. I don't know how to do this because every time I try, it only ends up looping once and not displaying the proper amount of coins.
Please help.
Here is my code:
import java.util.Scanner;
public class MakingChange {
public static void main(String[] args) {
//Prompts user to input the change they recieved.
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25 would be entered as 125.");
int Change = sc.nextInt(); //The value is then stored as an integer named Change.
int Pennies = 0;
int Nickels = 0;
int Dimes = 0;
int Quarters = 0;
while (Change < 1){
System.out.println("Error: Cannot enter a value less than 1!");
//System.exit(0); //found at http://codingforums.com/java-jsp/69296-%5Bjava%5D-how-end-program.html
}
while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
if (Change >= 25){
Change -= 25;
Quarters++;
}
else if (Change >= 10){
Change -= 10;
Dimes++;
}
else if (Change >= 5){
Change -=5;
Dimes++;
}
else if (Change >= 1){
Change -= 1;
Pennies++;
}
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", Quarters);
System.out.printf("Number of Dimes: %6d %n", Dimes);
System.out.printf("Number of Nickels: %4d %n", Nickels);
System.out.printf("Number of Pennies: %4d %n", Pennies);
//Prints out final number of coins used by type of coin.
}
}
Scrap your first while loop. You don't want two loops handling the same thing. Next, initialize your "Change" variable, but move sc.nextInt(); inside so that it gets the input every iteration. Finally, make the loop a do-while loop so it runs at least once (alternatively you could initialize change to 1).
int Change = 0;
do{
Change = sc.nextInt();
// ...
}
while(Change > 0);
You need to ask the user how much change they get in the loop. Here is some code below:
import java.util.scanner;
public void MakingChange {
public static void main(String[] args) {
int change = 0;
int currentchange = 0;
Scanner in = new Scanner(System.in);
do {
change += currentchange;
System.out.println("Enter an amount of change.");
currentchange = in.nextInt();
} while(currentchange > 0);
}
}
System.out.println("Total change: " + change);
+1 To Gary for do loop idea.
You get the point.
I see a few issues in your code, Java naming conventions have variables start with a lower case letter. Next, you could simply loop while the Scanner.hasNextInt(). Then, your code would seem to be simpler if you counted the quarters, dimes and nickels (which your nickel counter counts as dimes) in their own loops. So, something like -
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String msg = "Please enter the amount of change "
+ "you received in coin format. Example: "
+ "$1.25 would be entered as 125.";
System.out.println(msg);
while (sc.hasNextInt()) {
int change = sc.nextInt();
int pennies = 0;
int nickels = 0;
int dimes = 0;
int quarters = 0;
if (change < 1) {
System.out.println("Error: Cannot enter a value less than 1!");
} else {
while (change >= 25) {
change -= 25;
quarters++;
}
while (change >= 10) {
change -= 10;
dimes++;
}
while (change >= 5) {
change -= 5;
nickels++;
}
pennies = change;
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", quarters);
System.out.printf("Number of Dimes: %6d %n", dimes);
System.out.printf("Number of Nickels: %4d %n", nickels);
System.out.printf("Number of Pennies: %4d %n", pennies);
System.out.println(msg);
}
}
I would simplify your loop like so:
int change;
while ((change = sc.nextInt()) >= 0) {
//do code
}
Note the lowercase change, due to Java naming conventions.
The main problem with your code as-is is that you have two loops, and you'll exit the first immediately if your number is not less than 1 (or loop infinitely if it is less). You did the verification once, then did your program logic. Try using a single loop.
you need a loop around everything. The top of the loop will prompt and read the value, and the bottom of the loop will print the output. But because it is a loop, it will go back and prompt again. You don't have a loop at that level in the code as given.
import java.util.Scanner;
public class MakingChange {
public static void main(String[] args) {
//Prompts user to input the change they recieved.
Scanner sc = new Scanner(System.in);
int input = 0;
do{
System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25 would be entered as 125.");
input= sc.nextInt(); //The value is then stored as an integer named Change.
if(input<=0) {
System.out.println("Good Bye");
break; breaks the loop
}
else{
Change=input;
int Pennies = 0;
int Nickels = 0;
int Dimes = 0;
int Quarters = 0;
while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
if (Change >= 25){
Change -= 25;
Quarters++;
}
else if (Change >= 10){
Change -= 10;
Dimes++;
}
else if (Change >= 5){
Change -=5;
Dimes++;
}
else if (Change >= 1){
Change -= 1;
Pennies++;
}
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", Quarters);
System.out.printf("Number of Dimes: %6d %n", Dimes);
System.out.printf("Number of Nickels: %4d %n", Nickels);
System.out.printf("Number of Pennies: %4d %n", Pennies);
//Prints out final number of coins used by type of coin.
}//end of else
}while(input>0);//end of do while
}
Just make one while loop and keep referring to it until input < 0. So technically, all your calculations should go inside this huge while loop

min change greedy algorithm in java

Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's supposed to look like this:
> run Coins
Enter the amount of given money:
[1.73]
Give the seller 8 coins:
1 dollars,
2 quarters,
2 dime,
0 nickels,
3 pennies.
This is What I have so far:
import java.util.Scanner;
class Coins {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
double money;
System.out.println("Enter the amount of money ");
money = input.nextDouble();
while (money > 0.0 ){
if (money >= 1) {
System.out.println(money/1+"dollars");
money -= 1;
}
else if (money>=.25) {
System.out.println(money/.25+"quarters");
money-=.25;
}
else if (money>=.10) {
System.out.println(money/.10+"Dimes");
money-=.10;
}
else if (money>=.05) {
System.out.println(money/.05+"Nickels");
money-=.05;
}
else if (money>=.01) {
System.out.println(money/.01+"Penny");
money-=.01;
}
}
}
}
The part I need help with is this: If I run the program and enter the amount 1.73, the way I have the code written, it takes the number 1.73, divides it by 1, and prints "1.73 dollars". I need a way to get rid of the decimal part so instead of printing "1.73 dollars", it prints "1 dollar". But I'm not sure how to do this. I tried converting it to an int but it messes up the flow of the other statements. Please help me.
You should use the combination of floor with casting to double, the following code works:
class Coins {
public static void main (String args[]) {
double money = 1.73;
while (money > 0.0 ){
if (money >= 1) {
System.out.println(Math.floor(money/1)+" dollars");
money -= Math.floor(money/1)*(double)1;
}
else if (money>=.25) {
System.out.println(Math.floor(money/.25)+" quarters");
money-=Math.floor(money/.25)*(double).25;
}
else if (money>=.10) {
System.out.println(Math.floor(money/.10)+" Dimes");
money-=Math.floor(money/.10)*(double).10;
}
else if (money>=.05) {
System.out.println(Math.floor(money/.05)+" Nickels");
money-=Math.floor(money/.05)*(double).05;
}
else if (money>=.01) {
System.out.println(Math.round(money/.01)+" Penny");
money-=Math.round(money/.01)*(double).01;
}
}
}
}
Another bug you had:
You should subtract Math.floor(money/XXX)*(double)XXX not (double)XXX
You need get rid of the remainder after the divisions. You can use Math.floor() for this:
class Coins {
public static void main (String args[]) {
double money = 1.73;
int dollars = (int) Math.floor(money/1);
money -= dollars * 1;
int quarters = (int) Math.floor(money/0.25);
money -= quarters * 0.25;
int dimes = (int) Math.floor(money/0.10);
money -= dimes * 0.10;
int nickels = (int) Math.floor(money/0.05);
money -= nickels * 0.05;
int pennies = (int) Math.round(money * 100);
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
}
}
Resulting in:
Dollars: 1
Quarters: 2
Dimes: 2
Nickels: 0
Pennies: 3

calculating interest on a certificate of deposit

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++;
}

Categories