我的美元计算器在java中的工作方式与我想要的完全不同?

问题描述 投票:1回答:1
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不小于或等于零所以它应该转到else语句。

java jgrasp
1个回答
1
投票

你应该写

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

既然你写了a-b,那么答案将会有-1的值,因为-1<=0肯定会输入第一个if语句。

© www.soinside.com 2019 - 2024. All rights reserved.