简体   繁体   English

计算货币金额的硬币适用于某些值,但不适用于其他值

[英]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? (忽略用户插入负数或小数的事实。我只需要在 int 中使用它。)我的问题是它没有处理这些数字:(1,2,3,4, 20,21,22,23,24, 25,26,27,28,29,...等)为什么?

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();
        }
    }
}

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.首先,打印结果的代码放置在计算镍币的同一个 while 循环中。 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.显示便士时,您错误地将两个对pennies引用加在一起,导致应为数量的两倍。

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.最后,为了清楚起见,我建议使用 -1 作为退出程序的条件,而不是 0,因为每次循环运行时, change = 0在某个时刻为真,虽然循环不会中断,但它令人困惑。

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();
        }
    }
}

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.然后重置这些值,然后你应该就好了。

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();
        }
        
        
    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM