2014-01-26 37 views
-2

爲什麼java把一個否則會變成10的數字變成類似9.999999999999998的東西? 這是我的代碼。此外,任何建議良好的做法,將不勝感激。例如,我能做些什麼來代替所有的if語句。我的代碼的目的是接受什麼成本和多少支付的輸入。輸出將是差異,並且產生的美元和必須返還的硬幣。這裏是我遇到困難的代碼。爲什麼java把整數變成了更少的東西

package another; 

import javax.swing.JOptionPane; 

/** 
* 
* @author Will 
*/ 
public class Another { 

    public static void tryAgain() { 
     JOptionPane.showMessageDialog(null, "Enter a valid amount and try again"); 
     System.exit(0); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     String purchase = JOptionPane.showInputDialog("Please enter a the purchase amount"); 

     if (purchase == null) { 
      System.exit(0); 
     } 

     if (purchase.isEmpty()) { 
      tryAgain(); 
     } 

     for (int i = 0; i < purchase.length(); i = i + 1) { 
      if (!Character.isDigit(purchase.charAt(i)) 
        && !purchase.contains(".")) { 
       tryAgain(); 
      } 
     } 

     String given = JOptionPane.showInputDialog("Please enter the given amount"); 

     if (given == null) { 
      System.exit(0); 
     } 

     if (given.isEmpty()) { 
      tryAgain(); 
     } 

     for (int i = 0; i < given.length(); i = i + 1) { 
      if (!Character.isDigit(given.charAt(i)) 
        && !given.contains(".")) { 
       tryAgain(); 
      } 
     } 

     Double a = Double.parseDouble(purchase); 
     Double b = Double.parseDouble(given); 
     Double c = b - a; 

     if (c < 0) { 
      JOptionPane.showMessageDialog(null, "Give the cashier more money!"); 
      System.exit(0); 
     } 

     System.out.println("Change: $" + c); 

     if (c > 100) { 
      double hundredsPlusExtra = c/100; 
      double hundredsWithout = Math.floor(hundredsPlusExtra); //this line rounds down 
      c = c - hundredsWithout * 100; 
      if (hundredsWithout == 1) { 
       System.out.println(hundredsWithout + " One-Hundred Dollar Bill"); 
      } else { 
       System.out.println(hundredsWithout + " Hundred Dollar Bills"); 
      } 
     } 

     if (c > 50) { 
      double fiftiesPlusExtra = c/50; 
      double fiftiesWithout = Math.floor(fiftiesPlusExtra); //this line rounds down 
      c = c - fiftiesWithout * 50; 
      if (fiftiesWithout == 1) { 
       System.out.println(fiftiesWithout + " Fifty Dollar Bill"); 
      } else { 
       System.out.println(fiftiesWithout + " Fifty Dollar Bills"); 
      } 
     } 

     if (c > 20) { 
      double twentiesPlusExtra = c/20; 
      double twentiesWithout = Math.floor(twentiesPlusExtra); //this line rounds down 
      c = c - twentiesWithout * 20; 
      if (twentiesWithout == 1) { 
       System.out.println(twentiesWithout + " Twenty Dollar Bill"); 
      } else { 
       System.out.println(twentiesWithout + " Twenty Dollar Bills"); 
      } 
     } 

     if (c > 10) { 
      double tensPlusExtra = c/10; 
      double tensWithout = Math.floor(tensPlusExtra); //this line rounds down 
      c = c - tensWithout * 10; 
      if (tensWithout == 1) { 
       System.out.println(tensWithout + " Ten Dollar Bill"); 
      } else { 
       System.out.println(tensWithout + " Ten Dollar Bills"); 
      } 

     } 

     if (c > 5) { 
      double fivesPlusExtra = c/5; 
      double fivesWithout = Math.floor(fivesPlusExtra); //this line rounds down 
      c = c - fivesWithout * 5; 
      if (fivesWithout == 1) { 
       System.out.println(fivesWithout + " Five Dollar Bill"); 
      } else { 
       System.out.println(fivesWithout + " Five Dollar Bills"); 
      } 

     } 

     if (c > 1) { 
      double onesPlusExtra = c/1; 
      double onesWithout = Math.floor(onesPlusExtra); //this line rounds down 
      c = c - onesWithout * 1; 
      if (onesWithout == 1) { 
       System.out.println(onesWithout + " One Dollar Bill"); 
      } else { 
       System.out.println(onesWithout + " One Dollar Bills"); 
      } 
     } 

     if (c > .25) { 
      double quartersPlusExtra = c/.25; 
      double quartersWithout = Math.floor(quartersPlusExtra); //this line rounds down 
      c = c - quartersWithout * .25; 
      if (quartersWithout == 1) { 
       System.out.println(quartersWithout + " Quarter"); 
      } else { 
       System.out.println(quartersWithout + " Quarters"); 
      } 
     } 
    } 
} 
+0

如何在二進制表示十進制數來看看? –

+0

我在代碼中看到的唯一整數是'for'循環索引和整數文字。 –

+2

[什麼每個計算機科學家應該知道關於浮點](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) –

回答

1

我承認,我沒有閱讀您的代碼,但我99%確定您的二進制數據存儲存在問題。

您看到,在十進制系統中,1/10可以很容易地表示爲0.1。然而,嘗試1/3,成爲0.33333333333333333 ...

在二進制1/10是0.0001100110011001100110011 ...

查找到BigDecimal達到base10 numberstorage。

+0

在這個和其他許多簡單的例子中,在每個輸出完成這個工作之前正確舍入。 – maaartinus

相關問題