Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换装置故障_Java - Fatal编程技术网

Java 转换装置故障

Java 转换装置故障,java,Java,我很难理解如何将金额(如1.17美元)转换为该输出: 1美元 一角 1镍币 2便士 我需要使用if语句,我可以计算出来,但是,我的问题是试图正确显示变化量。这是我的密码。我是一个视觉学习者,所以如果你让我朝着正确的方向开始,那会很有帮助 import java.util.Scanner; public class ComputeChange { public static void main(String[] args) { Scanner input = new

我很难理解如何将金额(如1.17美元)转换为该输出:

  • 1美元
  • 一角
  • 1镍币
  • 2便士
我需要使用if语句,我可以计算出来,但是,我的问题是试图正确显示变化量。这是我的密码。我是一个视觉学习者,所以如果你让我朝着正确的方向开始,那会很有帮助

import java.util.Scanner;

public class ComputeChange {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an amount in double, for example 11.56: " );
        double number = input.nextDouble();
        System.out.println("Your amount " + number + " consists of ");
        int Dollars = (int) (number);
        int Quarters = Dollars / 25;

        if (number == 1) {
                    System.out.print("1 dollar ");
        }
            else if (number > 1) {
                System.out.print((int)Dollars + " dollars ");
            }
            if (number == 0) {
                System.out.println("");
            }
            System.out.print( Quarters + " Quarters ");
    }
}

您可能需要使用模数运算符
%
。它与2个数字一起使用,并返回
a
除以
b
的余数,其中
a
是左手赋值,
b
是右手赋值,如
a%b

例如:

11%2=1
说明:
5*2=10
11-10=1

.66%.25=.16
说明:
2*.25=.5
.66-.5=.16


从一个简单的问题开始,你只有两种类型的硬币,比如说
$0.08

double monies = .08;
int numNickles = (int)(monies/.05) = 1 // one nickle
monies = monies % .05; // or you can write as monies %=.05;
// monies value should now be .03
int numPennies = (int)(monies/.01) = 3 // three pennies

一种比使用模数更简单的方法是,自上而下(从最高到最低)计算测量单位,并从已转换为最低单位的总量中不断扣除

许多库也将这种方法与时间单位结合使用,即将时间跨度转换为小时、分钟和秒。对于货币,这里有同样的方法。我添加了内联注释以尽可能最好地解释代码

// Scan the amount
Scanner scanner = new Scanner(System.in);
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.close();

// convert into cents 
int cents = (int) (amount * 100);

// get dollars
int dollars = cents/100;
// cents left after dollars
cents = cents - dollars*100;

// get quarters
int quaters = cents/25;
// cents left after quarters
cents = cents - quaters*25;

// get dimes
int dimes = cents/10;
// cents left after dimes
cents = cents - dimes*10;

// get nickels
int nickels = cents/5;
// cents left after nickels
cents = cents - nickels*5;

// leftover cents are pennies
int pennies = cents;
现在,只需使用
StringBuilder
作为

StringBuilder msg = new StringBuilder("You have:");
if (dollars > 0) {
    msg.append(" ").append(dollars).append(" dollar").append(dollars > 1 ? "s" : "");
}
if (quaters > 0) {
    msg.append(" ").append(quaters).append(" quarter").append(quaters > 1 ? "s" : "");
}
if (dimes > 0) {
    msg.append(" ").append(dimes).append(" dime").append(dimes > 1 ? "s" : "");
}
if (nickels > 0) {
    msg.append(" ").append(nickels).append(" nickel").append(nickels > 1 ? "s" : "");
}
if (pennies > 0) {
    msg.append(" ").append(pennies).append(" pennie").append(pennies > 1 ? "s" : "");
}

System.out.println(msg);
输出:


谢谢你的视觉帮助,我只是想弄清楚如何把它放到嵌套的if语句形式中。
Enter amount: 1.17
You have: 1 dollar 1 dime 1 nickel 2 pennies

Enter amount: 12.99
You have: 12 dollars 3 quarters 2 dimes 4 pennies