double mny = 0;
mny = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter the amount of Money", "Money Input", JOptionPane.QUESTION_MESSAGE));
//Check for Tens
System.out.println("The # of Tens is " + (int)(mny/10));
mny = mny%10;
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
//Check for Fives
System.out.println("The # of Fives is " + (int)(mny/5));
mny = mny%5;
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
//Check for Pennies
System.out.println("The # of Pennies is " + (int)(mny/0.01));
mny = mny%0.01;
System.out.println("The remaining amount of money is " + new DecimalFormat("0.00").format(mny) + " $ ");
}//end class
如果我输入0.01,它会给我1便士,但是如果我输入15.01,它会给我1 10 1 5便士,但是没有便士,我该怎么解决这个问题呢?
2条答案
按热度按时间6mw9ycah1#
你所看到的问题是为什么它是非常不鼓励使用
double
货币价值。使用BigDecimal
相反。测验
输出
qfe3c7zg2#
因为您要将每个比较的结果分配给
mny
变量,请尝试不使用它们: