-2

我试图取一些零钱,小于 1.00 美元,但大于零,并显示进行该更改所需的硬币、硬币、镍币和便士的数量。输出必须以类似于英语的描述性方式呈现,并具有以下语法规则:

如果金额为零,则应使用"否"一词。如果金额为 1,则单位必须是单数。如果数量不是 1,则单位必须是复数。

此外,当输入小于 0 或大于 1 的任何内容时,程序应捕获用户错误,并进入数据验证循环以显示提示并要求用户重新输入金额。这个循环将一直持续到用户输入的金额小于 1.00 美元,但大于零。

当我运行我的代码时,我不断地得到4

谢谢你的时间!非常感谢任何帮助!

到目前为止我的代码:

import java.util.Scanner;

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

           Scanner keyboard = new Scanner(System.in); 

           System.out.print("Change in Coins\n" +
                           "----------------\n\n");

           System.out.println("Enter the amount less than $1.00, but\n" +
                            "more than zero.");

           System.out.print("\nEnter amount: ");
                    amt = keyboard.nextInt();

    // ----------------------------------------------             

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

           quarter = cents/25;
           cents %= 25;

           dime = cents/10;
           cents %= 10;

           nickle = cents/5;
           cents %= 5;

           penny = cents;

    // -----------------------------------------

           while(amt > 0 && amt <= 1)
           {

    // ----------------------------------------- 

             if(quarter == 0)
             {
                System.out.print("no ");
             }
             else
                System.out.print(quarter);
             }
             if(quarter == 1)
             {
                System.out.print("quarter");
             }
             else
             {
                System.out.print("quarters");
             }



           }           
    }
4

5 回答 5

2

您已声明amtint改为使用float

float amt = 0.0;
int cents, quarter, dime, nickle, penny;

// Get amt from user between 0.0 and 1.0

cents = (int)amt*100;

quarter = cents/25;
cents -= quarter*25;

dime = cents/10;
cents -= dime*10;

nickel = cents/5;
cents -= nickel*5;

penny = cents;

// Then print out the numbers of each

/*
if amt = 0.91

cents = 91

quarter = 3
cents = 16

dime = 1
cents = 6

nickel = 1
cents = 1

penny = 1
*/

使用模 %

float amt = 0.0;
int cents, quarter, dime, nickle, penny;

// Get amt from user between 0.0 and 1.0

cents = (int)amt*100;

quarter = cents/25;
cents = cents % 25;

dime = cents/10;
cents = cents % 10;

nickel = cents/5;
cents %= 5;

penny = cents;

// Then print out the numbers of each

完整程序

import java.util.Scanner;

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

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Change in Coins\n" + "----------------\n\n");

        System.out.println("Enter the amount less than $1.00, but\n"
                + "more than zero.");

        System.out.print("\nEnter amount: ");

        amt = keyboard.nextFloat();
        keyboard.close();

        cents = (int) (amt * 100);

        quarter = cents / 25;
        cents = cents % 25;

        dime = cents / 10;
        cents = cents % 10;

        nickle = cents / 5;
        cents %= 5;

        penny = cents;

        System.out.println("$" + amt + " = " + quarter + " quarters, " + dime
                + " dimes, " + nickle + " nickles and " + penny + " pennies");

    }
}

测试一下:http: //ideone.com/4Xq466

于 2013-09-26T01:45:07.100 回答
0

您的代码工作正常。唯一的问题是您最后会陷入循环,因为 amt 始终是原始数量,因此它会永远打印输出。

例如,如果要输出,

四分之一毛钱便士便士

然后你需要在你去的时候减少amt。

编辑**好吧...看起来很奇怪,就像您只是在计算使用的季度数一样。但问题是一样的。我不确定为什么你甚至需要一个循环。

于 2013-09-26T01:53:00.653 回答
0

你可以使用keyboard.nextDouble()。您应该使用 BigDecimal 进行货币比较。试试下面的代码:

   public class CoinChange {    

    public static void main(String[] args) {    
        double amt = getValidInputFromUser();

        int remainingCents = (int) (amt * 100);         

        int quarters = remainingCents / 25;
        remainingCents %= 25;

        int dimes = remainingCents / 10;
        remainingCents %= 10;

        int nickles = remainingCents / 5;
        remainingCents %= 5;

        int pennies = remainingCents;

        System.out.println(String.format(
                    "Quarters %d, Dimes %d, Nickles %d, Cents %d", quarters,
                    dimes, nickles, pennies));      
    }


    private static double getValidInputFromUser() {
        double amt = BigDecimal.ZERO.doubleValue();
        do {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Change in Coins\n" + "----------------\n\n");
            System.out.println("Enter the amount less than $1.00, but\n"
                    + "more than zero.");
            System.out.print("\nEnter amount: ");
            amt = keyboard.nextDouble();
        } while (amountGreaterThanOne(amt) || amountIsZero(amt));
        return amt;
    }


    private static boolean amountIsZero(double amt) {
        return (BigDecimal.valueOf(amt).compareTo(BigDecimal.ZERO) == 0);
    }


    private static boolean amountGreaterThanOne(double amt) {
        return BigDecimal.valueOf(amt).compareTo(BigDecimal.ONE) > 0;
    }
}
于 2013-09-26T03:22:34.593 回答
0

Scanner.nextInt()返回一个整数。如果您希望用户输入 1 到 99 的整数便士,那么您应该将其分配给美分。你把自己弄糊涂了amt

于 2013-09-26T01:59:00.143 回答
0

我的猜测是您输入的数字是 1.05、1.15 等。如果您将其输入为 amt,则乘以 100(得到总美分)并加上 0.1(不确定您为什么这样做)。然后当你真的想要一个双精度时转换成一个 int 。由于您转换为 int,因此 cents / 25 通常为 4,例如,115/25 除以四次,其余部分被丢弃,因此为什么您一直得到 4 作为答案。

于 2013-09-26T01:44:12.377 回答