猿问

java中排序更改为小数的问题

您好,我需要帮助将零钱排序为小数,如 25 美分、便士、镍币等。它说可能有损转换。这是我的代码:


public class Assignment04 {

    public static void main(String[] args) {

        Scanner stdin = new Scanner(System.in);

        System.out.println("Enter your amount");

        long amount = stdin.nextLong();

        long remainder = Math.round(amount * 100 );



        long hundreds = (int) remainder / 10000;

        remainder = remainder % 10000;


        long fifties = (int) remainder / 5000;

        remainder = remainder % 5000;


        long twenties = (int)remainder / 2000;

        remainder = remainder % 2000;


        long tens = (int)remainder / 1000;

        remainder = remainder % 1000;


        long fives = (int)remainder / 500;

        remainder = remainder % 500;


        long ones = (int)remainder / 100;

        remainder = remainder % 100;


        long quarters = remainder / .25;

        remainder = remainder % .25;


        long dimes =  (int)remainder / .10;

        remainder = remainder % .10;


        long nickels =  (int)remainder / .5;

        remainder = remainder % .5;


        double pennies = (int) remainder;





        System.out.println(hundreds + "hundred/s");

        System.out.println(fifties + "fiftie/s");

        System.out.println(twenties + "twentie/s");

        System.out.println(tens + "ten/s");

        System.out.println(fives + "five/s");

        System.out.println(ones + "one/s");

        System.out.println(quarters + "quarter/s");

        System.out.println(dimes + "dime/s");

        System.out.println(nickels + "nickel/s");

        System.out.println(pennies + "cent/s");


    }

}

我试图弄清楚如何删除红色下划线,以便它可以正确运行。我已经完成了大部分工作,只是当涉及到小数时我感到困惑。


一只甜甜圈
浏览 96回答 3
3回答

慕标5832272

您的余数字段似乎以美分表示。$100 = 10000$20 = 2000$1 = 100因此,四分之一应为 25,一角应为 10,五分镍应为 5。还要更改此:double pennies = (int) remainder;对此:long pennies = remainder;修复扫描仪输入的 amount 并删除所有最终存储在 long 中的行的 (int) 转换:public class Assignment04 {    public static void main(String[] args) {        Scanner stdin = new Scanner(System.in);        System.out.println("Enter your amount");        long remainder = Math.round(stdin.nextDouble() * 100 );        long hundreds = remainder / 10000;        remainder = remainder % 10000;        long fifties = remainder / 5000;        remainder = remainder % 5000;        long twenties = remainder / 2000;        remainder = remainder % 2000;        long tens = remainder / 1000;        remainder = remainder % 1000;        long fives = remainder / 500;        remainder = remainder % 500;        long ones = remainder / 100;        remainder = remainder % 100;        long quarters = remainder / 25;        remainder = remainder % 25;        long dimes = remainder / 10;        remainder = remainder % 10;        long nickels = remainder / 5;        remainder = remainder % 5;        long pennies = remainder;        System.out.println(hundreds + "hundred/s");        System.out.println(fifties + "fiftie/s");        System.out.println(twenties + "twentie/s");        System.out.println(tens + "ten/s");        System.out.println(fives + "five/s");        System.out.println(ones + "one/s");        System.out.println(quarters + "quarter/s");        System.out.println(dimes + "dime/s");        System.out.println(nickels + "nickel/s");        System.out.println(pennies + "cent/s");    }}

慕容3067478

你可以使用 int 。而不是长。我希望这有帮助。谢谢Scanner stdin = new Scanner(System.in);            int amount;            int remainder;            System.out.print("Enter amount:");            amount= (int) Math.round(stdin.nextDouble() * 100);            int hundreds= remainder / 1000;                if (hundreds > 0) {                    remainder = remainder% 1000;                    System.out.println(hundreds + "hundred/s");                }     int fifties = remainder / 1000;                        if (fifties > 0) {                            remainder = remainder% 1000;                            System.out.println(fifties + "fiftie/s");                    }                // number of twenties for remainder.                int twenties = change / 2000                if (twenties > 0) {                     remainder= remainder % 2000; // this resets the value of remainder to                    // the remainder after the twenties are                    // calculated but only if there was at                    // least enough to make one twenty                    System.out.println(twenties + "twentie/s");                }                int tens = remainder / 1000;                if (tens > 0) {                    remainder = remainder% 1000;                    System.out.println(tens + " ten/s");                }                int fives = remainder/ 500;                if (fives > 0) {                    remainder= remainder% 500;                    System.out.println(fives + " five/s");                }                int ones = remainder/ 100;                if (ones > 0) {                    remainder = remainder % 100;                    System.out.println(ones + " one/s");                }                int quarters = remainder/ 25;                if (quarters > 0) {                    remainder= remainder% 25;                    System.out.println(quarters + " quarter/s");                }                int dimes = remainder/ 10;                if (dimes > 0) {                    remainder = remainder % 10;                    System.out.println(dimes + " dime/s");                }                int nickels = remainder/ 5;                if (nickels > 0) {                    remainder= change % 5;                    System.out.println(nickels + " nickel/s");                }                int pennies = remainder;                System.out.println(pennies + " cent/s");            }        }

不负相思意

我通过将金额和余数转换为双倍来修复它。
随时随地看视频慕课网APP

相关分类

Java
我要回答