Eduardo Alencar
Eduardo Alencar

Reputation: 13

Calculating coins for money amount works for some values, but not for others

I have a second program due tomorrow and I have been working non stop to try to understand why some numbers work and others don't.

The general idea is that a person enters an amount of 'change', the program will run and separate that change into 'quarters', 'dimes', 'nickels', and 'pennies'. and this will keep happening until the person enters a zero instead of an integer. (Ignore the fact of the user inserting negative, or decimals. I just need this in int.) My problem is that it's not processing these numbers: (1,2,3,4, 20,21,22,23,24,25,26,27,28,29,... etc.) Why?

Here is my code:

import java.util.Scanner; //I have established and imported the Scanner in this line!
    
public class Main{
    public static void main(String[] args) {
            
        // bellow is where the variables are declared!
        int change = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
            
        // change inquiry in the following line 
        System.out.println("Please Enter an amount of change greater than zero or enter Zero to exit: ");
            
        //declaring the scanner variable and  preparing it for use 
        Scanner myObj = new Scanner(System.in);
        change = myObj.nextInt(); //change input is inserted by user
            
        //if the change is not equal to zero then it will 
        //seperate the nuber into its respective coin count.
        while (change != 0){
            while (change >= 25){
                change = change - 25;
                quarters = quarters + 1;
            }
            while (change >= 10){
                change = change - 10;
                dimes = dimes + 1;
            }
            while (change >= 5){
                change = change - 5;
                nickels = nickels + 1;
                                   
                pennies = change;
                                    
                //finally ready to show the work the 
                //computer performed  with these print statements
                System.out.println("There are (is): " + quarters +" quarter(s),");
                System.out.println(dimes + " dime(s), ");
                System.out.println(nickels + " nickel(s), and ");
                System.out.println(pennies + pennies + " pennies left");
                                    
    
            }
            //this question will continue to be 
            //asked untill
            //Zero is entered which will then end 
            //the program.
            System.out.println("Please Enter a new amount of change greater than zero or enter Zero to exit: ");
            change = myObj.nextInt();
        }
    }
}

Upvotes: 0

Views: 534

Answers (3)

Eduardo Alencar
Eduardo Alencar

Reputation: 13

I Appreciate all that you guys have done to help me! The code has been fixed! And a ton of anxiety and frustration has been removed from my shoulders! I appreciate you all. Im glad I found this website today! Here is the updated code and it performs exactly as designed in my assignment. I normally wouldnt include so many coments but i am getting graded on them as well so Here is the updated code! again Thank you all who hleped me!

import java.util.Scanner; //I have established and imported the Scanner in this line!

public class Main{
    public static void main(String[] args) {
        
        // bellow is where the variables are declared!
        int change = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
        
        // change inquiry in the following line 
        System.out.println("Please Enter an amount of change greater than zero or enter Zero to exit: ");
        
        //declaring the scanner variable and  preparing it for use 
        Scanner myObj = new Scanner(System.in);
        change = myObj.nextInt(); //change input is inserted by user
        
        //if the change is not equal to zero then it will 
        //seperate the nuber into its respective coin count.
        while (change != 0){
            while (change >= 25){
                change = change - 25;
                quarters = quarters + 1;
            }
            while (change >= 10){
                change = change - 10;
                dimes = dimes + 1;
            }        
            while (change >= 5){
                change = change - 5;
                nickels = nickels + 1;                
            }                   
                pennies = change;
             //finally ready to show the work the 
              //computer performed  with these print statements
            System.out.println("There are (is): " + quarters +" quarter(s),");
            System.out.println(dimes + " dime(s), ");
            System.out.println(nickels + " nickel(s), and ");
            System.out.println( pennies + " pennies left");                                     
                               
            change = 0;
            quarters = 0;
            dimes = 0;
            nickels = 0;
            pennies = 0;                

                            
        //this question will continue to be 
        //asked untill
        //Zero is entered which will then end 
        //the program.
       System.out.println("Please Enter a new amount of change greater than zero or enter Zero to exit: ");
         change = myObj.nextInt();
        }
        
        
    
    }
}

Upvotes: 0

Ranwolf Surveyir
Ranwolf Surveyir

Reputation: 23

First of all, the nickels loop should only calculate for nickels, not output the result.

Second, the values of each coin are not reset, so they will accumulate over time.

Here's an example of how you can do it:

            while (change >= 5){
            change = change - 5;
            nickels = nickels + 1;
        }

And then reset the values, and then you should be good.

Upvotes: 2

quartzic
quartzic

Reputation: 309

There are a couple logic errors in your code; first, your code to print the results is placed in the same while loop that counts nickels. If there is no need for a nickel to be counted, that code is never reached. Pennies are the same way. Therefore, any value of change that can be reached without using any nickels will error.

You also do not reset the values of each coin when asking for a new change value, so coins will add up over time. You can solve this by declaring the variables inside the loop, not outside.

When displaying pennies, you mistakenly add together two references to pennies, resulting in twice the amount that it should be.

Finally, for clarity, I recommend using -1 as your condition to exit the program, not 0, because change = 0 is true at some point every time the loop runs and although the loop will not break, it is confusing.

import java.util.Scanner;
    
public class Main{
    public static void main(String[] args) {
        int change = 0;
            
        // change inquiry in the following line 
        System.out.println("Please enter an amount of change or enter -1 to exit: ");
            
        //declaring the scanner variable and  preparing it for use 
        Scanner myObj = new Scanner(System.in);
        change = myObj.nextInt(); //change input is inserted by user
            
        // if the input is not equal to -1, count change
        while (change != -1){

            // Redeclare each coin's count value inside this loop so that you start from 0 each time.
            int quarters = 0;
            int dimes = 0;
            int nickels = 0;
            int pennies = 0;

            // Count quarters
            while (change >= 25){
                change = change - 25;
                quarters = quarters + 1;
            }
            // Count dimes
            while (change >= 10){
                change = change - 10;
                dimes = dimes + 1;
            }
            // Count nickels
            while (change >= 5){
                change = change - 5;
                nickels = nickels + 1;
            }
            // Count pennies          
            pennies = change;

            // Done counting! Display results:   
            System.out.println("There are (is): " + quarters +" quarter(s),");
            System.out.println(dimes + " dime(s), ");
            System.out.println(nickels + " nickel(s), and ");
            System.out.println(pennies + pennies + " pennies left");
                                
            // Ask for a new change value.
            System.out.println("Please enter a new amount of change or enter -1 to exit: ");
            change = myObj.nextInt();
        }
    }
}

Upvotes: 2

Related Questions