我的美元计算器在 java 中没有完全按照我想要的方式工作?


import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Part2 {
    public static void main(String []args) {
         Scanner input = new Scanner(System.in);
         NumberFormat money = NumberFormat.getCurrencyInstance();
         DecimalFormat decimalformat = new DecimalFormat("##0; ##0");
         double a, b, answer;
         double nOnebills, nFivebills, nTenbills;
         double nTwentybills, nFiftybills, nOnehundredbills;
         double int nPenny, nNickle, nDime, nQuarter;
         System.out.println("Please input the amount the customer owns.");
         a = input.nextDouble();
         System.out.println("Please input the amount the customer paid.");
         b = input.nextDouble();
         answer = a - b;
         System.out.println("The amount of change given back to the customer "+ 
         money.format(answer));
         nOnehundredbills = (answer)/(100);
         if (nOnehundredbills <= 0) {
             System.out.println("The number of hundred dollar bills in your change is 0"); 
         } else {
             System.out.println("The number of hundred dollar bills in your change is "+ nOnehundredbills);
         }
    }
}

输出是:

Please input the amount the customer owns.
100
Please input the amount the customer paid.
200
The amount of change given back to the customer ($100.00)
The number of hundred dollar bills in your change is 0

0应该是1,因为100除以100为1,而1不小于或等于零,因此应该转到其他语句。

你应该写

 answer =b-a;//not a-b 

由于您写了a-b,因此答案的值为-1,肯定会输入第一个IF语句,因为-1<=0

最新更新