2021-01-02 坚持坚持
程序员文章站
2022-06-19 19:14:15
import java.util.Scanner;public class ComputeChange { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter an amount in double, for example 11.56: "); double amount = inpu....
import java.util.Scanner;
public class ComputeChange {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount in double, for example 11.56: ");
double amount = input.nextDouble();
int remainingAmount = (int)(amount * 100);
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int numberOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
int numberOfPennies = remainingAmount;
System.out.println("Your amount " + amount + " consists of ");
System.out.println(" " + numberOfOneDollars + " dollars");
System.out.println(" " + numberOfQuarters + " quaters");
System.out.println(" " + numberOfDimes + " dimes");
System.out.println(" " + numberOfNickels + " nickels");
System.out.println(" " + numberOfPennies + " pennies");
}
}
今天大概学了30分钟
如果是dollar。那么就是double类型,可以接受小数部分,小数部分代表美分直,通过double转int则会造成精度确实。所以需要直接用美分为单位进行输入,及接受int类型值,直接输入1003就可以了。输出正确结果
本文地址:https://blog.csdn.net/wanshuai666/article/details/112125173
上一篇: 鳕鱼山药的做法和功效