【问题标题】:unable to get the correct output for input .11无法为输入 .11 获得正确的输出
【发布时间】:2014-03-06 05:34:22
【问题描述】:

我在学校有一个作业,我必须为用户输入的小于 1.00 但大于 0 的金额显示正确的更改。除了两位数中包含 1 的任何金额之外,每个金额都有效或第十位的6。例如 .11、.16、.21、.26 等。

这是我的代码

import java.util.Scanner;

public class AmountChange
{
   public static void main(String[] args)
   {
      //
      double amt; 
      int cents, quarter, dime, nickle, penny;

      Scanner keyboard = new Scanner(System.in);

      //To get the users input
      System.out.println("Change in Coins");
      System.out.println("---------------");
      System.out.println("Enter the amount less than $1.00, " +
                         "\nbut more than zero.");
      System.out.print("\nEnter amount: ");
      amt = keyboard.nextDouble();

      //Loop for incorrect input
      while ( amt < 0 || amt > 1.00 )
      {
         System.out.println("Please enter the amount less than $1.00,"  
                           + "\nbut more than zero.");
         System.out.print("\nRe-enter amount: ");
         amt = keyboard.nextDouble();
      }

      // 
      cents = (int)( amt * 100 + .1 );

      quarter = cents/25;
      cents %= 25;

      dime = cents/10;
      cents %= 10;

      nickle = cents/5;
      cents %= 5;

      penny = cents;

      // ----------------------------------------------------------
      if (quarter > 1)
      {
         System.out.print("\nYou will need " + quarter + " quarters, ");
      }
      else if (quarter == 1)
      {
         System.out.print("\nYou will need " + quarter + " quarter ,");
      }
      else 
      {
         System.out.print("\nYou will need no quarters, ");
      }

      // ----------------------------------------------------------
      if (dime > 1)
      {
         System.out.print(dime + " dimes, ");
      }
      else if (dime == 1)
      {
         System.out.print(dime + " dime, ");
      }
      else 
      {
         System.out.print("no dimes, ");
      }

      // ----------------------------------------------------------
      if (nickle > 1)
      {
         System.out.print(nickle + " nickles, ");
      }
      else if (nickle == 1)
      {
         System.out.print(nickle + " nickle, ");
      }
      else 
      {
         System.out.print("no nickles, ");
      }

      // ----------------------------------------------------------
      if (penny > 1)
      {
         System.out.print("and " + penny + " pennies.");
      }
      else if (quarter == 1)
      {
         System.out.print("and " + penny + " penny.");
      }
      else 
      {
         System.out.print("and no pennies.");
      }      
   }
}

【问题讨论】:

    标签: java floating-point modulus


    【解决方案1】:

    啊,剪切和粘贴的乐趣:-)

    if (penny > 1)
    {
        System.out.print("and " + penny + " pennies.");
    }
    else if (quarter == 1)  // <<<<< LOOK HERE !!!
    {
         System.out.print("and " + penny + " penny.");
    }
    else 
    {
         System.out.print("and no pennies.");
    }      
    

    应该是penny,而不是quarter

    事实上,它实际上确实适用于.26(尽管您有断言),因为quarter 设置为1,与penny 相同。事实上,它适用于任何四分之一数等于便士数的值(.26.52.78),但这只是偶然。


    顺便说一句,您可能需要考虑的另一件事是重构所有重复的代码,例如:

    import java.util.Scanner;
    
    public class Test
    {
        static double getAmount(Scanner keyboard) {
            System.out.println("Enter the amount between zero and $1.00.");
            System.out.print("\nEnter amount: ");
            return keyboard.nextDouble();
        }
    
        static String mkeTxt (int val, String prefix, String singular, String plural) {
            if (val == 0)
                return prefix + "no " + plural;
            if (val == 1)
                return prefix + "1 " + singular;
            return prefix + val + " " + plural;
        }
    
        public static void main(String[] args)
        {
            double amt; 
            int cents, quarter, dime, nickle, penny;
    
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Change in Coins");
            System.out.println("---------------");
            amt = getAmount(keyboard);
            while ( amt < 0 || amt > 1.00 )
                amt = getAmount(keyboard);
    
            cents = (int)( amt * 100 + .1 );
            quarter = cents/25;
            cents %= 25;
            dime = cents/10;
            cents %= 10;
            nickle = cents/5;
            cents %= 5;
            penny = cents;
    
            System.out.print("\nYou will need ");
            System.out.print(mkeTxt(quarter,"", "quarter", "quarters"));
            System.out.print(mkeTxt(dime,", ", "dime", "dimes"));
            System.out.print(mkeTxt(nickle,", ", "nickle", "nickles"));
            System.out.print(mkeTxt(penny," and ", "penny", "pennies"));
            System.out.println(".");
        }
    }
    

    使用函数来输出提示并接受输入使用户输入代码更易于维护,因为您只需要在一个地方更改交互。

    真正的节省是mkTxt() 函数,它为您提供一个自动调整硬币数量的字符串。它消除了main() 中大量的if/then/else 块,在一定程度上提高了可读性。

    如果您发现自己多次执行类似的操作但具有不同的值,那么肯定需要将其更改为某种描述的函数或循环。

    【讨论】:

      【解决方案2】:

      你只是有一个简单的错字! 变化:

      else if (quarter == 1){
          System.out.print("and " + penny + " penny.");
      } else {
          System.out.print("and no pennies.");
      }
      

      到,

      else if (penny == 1){
          System.out.print("and " + penny + " penny.");
      } else {
          System.out.print("and no pennies.");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-03
        • 2020-04-09
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 2012-11-19
        相关资源
        最近更新 更多