【问题标题】:Creating a coin calculation创建硬币计算
【发布时间】:2021-03-04 19:03:47
【问题描述】:

我正在创建一个硬币计算器,它将美分价值分解为面额,但我希望这样做,以便用户可以选择排除硬币的面额,请参阅下面的代码。我在这里碰壁并努力想出一个允许这种情况发生的代码,如果有人有指针,我将非常感激。我可以毫无问题地创建用户可以选择包含哪个面额的代码。

    int money;
        int denom;
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter the amount of coins you have in penny value");
        money = sc.nextInt();
        System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
        denom = sc.nextInt();
        while (money > 0){
            if (money >= 200) {
                System.out.println("£2");
                money -= 200;
            }
            else if (money >= 100) {
                System.out.println("£1");
                money -= 100;
            }
        else if (money >= 50) {
            System.out.println("50p");
            money -= 50;
            }
        else if (money >= 20) {
            System.out.println("20p");
            money -= 20;
            }
        else if (money >= 10) {
            System.out.println("10p");
            money -= 10;
        }       
        else if (money >= 1) {
            System.out.println("1p");
            money -= 1;
        
    }

【问题讨论】:

    标签: java eclipse loops if-statement switch-statement


    【解决方案1】:

    我想说这可能是最基本的方法,无需为其创建单独的方法。将面额设置为排除值,然后在减去之前检查硬币是否等于面额可以工作,因为反正没有那么多硬币。

    int money;
            int denom;
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter the amount of coins you have in penny value");
            money = sc.nextInt();
            System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
            String input = sc.next(); //saving user input as a string
            if (input.contains("p"))
            { //converting string to int by removing the p 
                denom = Integer.parseInt(input.substring(0, input.indexOf("p")));
            }
            else
            { //if there's no p, multiply its value by 100
                denom = Integer.parseInt(input) * 100;
            }
           
            while (money > 0){
                if (money >= 200 && denom != 200) {
                    System.out.println("£2");
                    money -= 200;
                }
                else if (money >= 100 && denom != 100) {
                    System.out.println("£1");
                    money -= 100;
                }
                else if (money >= 50 && denom != 50) {
                    System.out.println("50p");
                    money -= 50;
                    }
                else if (money >= 20 && denom != 20) {
                    System.out.println("20p");
                    money -= 20;
                    }
                else if (money >= 10 && denom != 10) {
                    System.out.println("10p");
                    money -= 10;
                }       
                else if (money >= 1) {
                    System.out.println("1p");
                    money -= 1;
            
                }
            }
    

    【讨论】:

    • 谢谢!我不知道我以前怎么没看到这个,你对我帮助很大:)
    • 没问题,乐于助人。
    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多