java 编写一个代码,以给予最少的硬币,为用户输入的数字,我不知道为什么结果没有打印出来

5rgfhyps  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(72)

我不知道如何得到正确的结果打印出来,也不知道为什么它不给正确的React时,它是。开始的代码是我被告知不要碰的启动代码。
我尝试移动print语句的位置,但它最终只打印了四分之一,其他什么都没有。

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    int pennies = 0;
        //prompt the user for a monetary input
        Scanner sc = new Scanner(System.in);
        double changeToGive = 0;
        
        do {
            //if the user gave us a negative dollar amount
            if(changeToGive < 0)
                System.out.println("Dollar amounts must be positive.");
            
            //if the current amount is zero dollars
            else if(changeToGive == 0)
                System.out.println("How much change do you need to pay out?");
            
            // while the user's input is not a valid double, prompt again
            while (!sc.hasNextDouble()) {
                System.out.println("Invalid input.  How much change do you need to pay out?");
                sc.next(); // this is important!
            }
            
            //if the user entered a valid double, assign it to changeToGive
            changeToGive = sc.nextDouble();
        } while (changeToGive <= 0); //if the user entered a valid double but it was negative, do it all again

     while (changeToGive > 0){ 
    if (changeToGive >= .25){
    changeToGive -= .25;
    quarters++;
    }else if (changeToGive >= .10){
    changeToGive -= .10;
    dimes++;
    }else if (changeToGive >= .5){
    changeToGive -= .5;
    nickels++;
    }else if (changeToGive >= .1){
    changeToGive -= .1;
    pennies++;
    }
     }
    System.out.print("Your change is:\n"+quarters+" quarters\n"+dimes+" dimes\n"+nickels+" nickels\n"+pennies+" pennies\n");
       
    
    
    

    }
    
      
    }
mgdq6dx1

mgdq6dx11#

在将changeToGive变成int而不是double(加上删除少量不必要的代码)之后,您将拥有一个工作解决方案:

int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
Scanner sc = new Scanner(System.in);
int changeToGive = 0;

do {
    // if the user gave us a negative dollar amount
    if (changeToGive < 0)
        System.out.println("Dollar amounts must be positive.");

    // prompt the user for a monetary input
    System.out.println("How much change do you need to pay out?");

    // while the user's input is not a valid double, prompt again
    while (!sc.hasNextDouble()) {
        System.out.println("Invalid input. How much change do you need to pay out?");
        sc.next(); // this is important!
    }

    // if the user entered a valid double, assign it to changeToGive
    changeToGive = (int) (sc.nextDouble() * 100); // cents
} while (changeToGive <= 0); //if the user entered a valid double but it was negative, do it all again

while (changeToGive > 0) {
    if (changeToGive >= 25) {
        changeToGive -= 25;
        quarters++;
    } else if (changeToGive >= 10) {
        changeToGive -= 10;
        dimes++;
    } else if (changeToGive >= 5) {
        changeToGive -= 5;
        nickels++;
    } else {
        changeToGive -= 1;
        pennies++;
    }
}

System.out.print("Your change is:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels\n" + pennies + " pennies\n");

对于奖励标记,使用enum的硬币,每个硬币都有value字段,而不是硬编码。交给你去调查

rmbxnbpk

rmbxnbpk2#

  • ".我不知道如何得到正确的结果打印出来,也不知道为什么它不给正确的React时,它是。..."*

您错误地将 5 cent1 cent 值表示为 0.50.1,而不是 0.050.01

if (changeToGive >= .5) changeToGive -= .5;
if (changeToGive >= .1) changeToGive -= .1;

此外,changeToGive 值在您扣除值时发生变化。
这是一个迭代 0.99

0.99 - .25 = 0.74
0.74 - .25 = 0.49
0.49 - .25 = 0.24
0.24 - .10 = 0.13999999999999999
0.13999999999999999 - .10 = 0.03999999999999998
0.03999999999999998 - 0.01 = 0.029999999999999978
0.029999999999999978 - 0.01 = 0.019999999999999976
0.019999999999999976 - 0.01 = 0.009999999999999976

按照 Java 的建议,将 BigDecimal 类用于 currency 应用程序。

这里有一个例子。

import static java.math.BigDecimal.ZERO;
import static java.math.RoundingMode.HALF_EVEN;

import java.math.BigDecimal;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
//prompt the user for a monetary input
Scanner sc = new Scanner(System.in);
BigDecimal changeToGive = ZERO.setScale(2, HALF_EVEN), z = ZERO,
           q = new BigDecimal(".25"), d = new BigDecimal(".10"),
           n = new BigDecimal(".05"), p = new BigDecimal(".01");

do {
    //if the user gave us a negative dollar amount
    if(changeToGive.compareTo(z) < 0)
        System.out.println("Dollar amounts must be positive.");

        //if the current amount is zero dollars
    else if(changeToGive.compareTo(z) == 0)
        System.out.println("How much change do you need to pay out?");

    // while the user's input is not a valid double, prompt again
    while (!sc.hasNextBigDecimal()) {
        System.out.println("Invalid input.  How much change do you need to pay out?");
        sc.next(); // this is important!
    }

    //if the user entered a valid double, assign it to changeToGive
    changeToGive = sc.nextBigDecimal();
} while (changeToGive.compareTo(z) <= 0); //if the user entered a valid double but it was negative, do it all again

while (changeToGive.compareTo(z) > 0){
    if (changeToGive.compareTo(q) >= 0){
        changeToGive = changeToGive.subtract(q);
        quarters++;
    }else if (changeToGive.compareTo(d) >= 0){
        changeToGive = changeToGive.subtract(d);
        dimes++;
    }else if (changeToGive.compareTo(n) >= 0){
        changeToGive = changeToGive.subtract(n);
        nickels++;
    }else if (changeToGive.compareTo(p) >= 0){
        changeToGive = changeToGive.subtract(p);
        pennies++;
    }
}
System.out.print("Your change is:\n"+quarters+" quarters\n"+dimes+" dimes\n"+nickels+" nickels\n"+pennies+" pennies\n");

输出

How much change do you need to pay out?
.99
Your change is:
3 quarters
2 dimes
0 nickels
4 pennies

相关问题