2014-09-10 140 views
0

當我轉換貨幣並將其分爲美元金額時,我正在努力尋找餘數。另外,創建一個最低數量的貨幣將被轉換是我的問題。我明白很多我的代碼是一團糟,但這是我的第一個Java項目。任何幫助將非常感激。Java貨幣轉換器(如何將int轉換爲Double +許多其他問題)

/* Currency Conversion 

*/ 

import java.util.Scanner;//to get keyboard input from user 

public class Currency { 

/** 
* Converts from a foreign currency to US Dollars. Takes a 
* deduction for the transaction fee. Displays how many of 
* each bill and coin denomination the user receives. 
* 
* @param args no parameters expected 
*/ 
public static void main(String[] args) 
{ 


    final double SEK_CONVERSION_RATE = .14; 

    /* 
    * You'll need a variable or constant for the transaction fee. 
    * (3% is fairly typical.) You'll also need a variable or 
    * constant for the minimum transaction fee. 
    */ 
    double transactionFee = .03; 
    final double MIN_TRANSACTION_FEE = 10.00; 

    /* 
    * You're going to need a variable for the amount of that 
    * currency the user wants to convert. 
    */ 

    //Before you can ask the user for input, you need a Scanner so you can read 
    //that input: 

    Scanner keyboard = new Scanner(System.in); 

    /* 
    * Now you're ready to interact with the user. You'll want 
    * to greet them and let them know what currency they can convert 
    * and what the rate of exchange is. 
    */ 

    System.out.print("Hello there, welcome to your one and only stop for Swedish Krona conversions. The exchange rate currently sits at .14"); 



    /* 
    * You should also let them know about the transaction fee. 
    */ 
    System.out.println(" The minimum transaction fee is $10.00 USD."); 





    /* 
    * Now you're ready to prompt the user for how much money they 
    * want to convert. 
    */ 
    System.out.println("How many Swedish Krona would you like to convert to US Dollar?"); 
    double userCurrencyInput = keyboard.nextDouble(); 
    /* 
    * And then use the Scanner to read in that input and initialize your 
    * variable for currency: 
    */ 
    double calculatedCurrInput = (userCurrencyInput*SEK_CONVERSION_RATE); 
    /* setting up casting to use later in program */ 
    int calculatedCurrInputInt = (int) (calculatedCurrInput); 



    /* 
    * You've received an amount in a foreign currency, but you're going 
    * to need the amount in dollars. Furthermore, you're going to need 
    * to know the number of 20's, 10's,5's, 1's, quarters, dimes, nickels 
    * and pennies. And you're going to need to know the transaction fee. 
    * These should all be stored in variables. 
    */ 
    double totalTransaction = (userCurrencyInput*transactionFee + calculatedCurrInput); 
    int totalTransactionInt = (int) (totalTransaction); 



/*Need to define the remainder correct to make change*/ 
    System.out.println("The conversion is " + calculatedCurrInput); 

    int twentyDollar = 20; 
    int twentyDollarBills = calculatedCurrInputInt/twentyDollar; 
    int remainderOfTwenty = calculatedCurrInputInt%twentyDollar; 

    int tenDollar = 10; 
    int tenDollarBills = remainderOfTwenty/tenDollar; 
    int remainderOfTen = remainderOfTwenty%tenDollar; 

    int fiveDollar = 5; 
    int fiveDollarBills = remainderOfTen/fiveDollar; 
    int remainderOfFive = remainderOfTen%fiveDollar; 

    int oneDollar = 1; 
    int oneDollarBills = remainderOfFive/oneDollar; 
    int remainderOfOnes = remainderOfFive%oneDollar; 
    double remainderOfOnesDBL = (double) remainderOfOnes; 

    double quarter = .25; 
    double numberOfQuarters = remainderOfOnesDBL/quarter; 
    double remainderOfQuarters = remainderOfOnesDBL%quarter; 

    double dimes = .10; 
    double numberOfDimes = remainderOfQuarters/dimes; 
    double remainderOfDimes = remainderOfQuarters%dimes; 

    double nickels = .05; 
    double numberOfNickels = remainderOfDimes/nickels; 
    double remainderOfNickels = remainderOfDimes%nickels; 

    double pennies = .01; 
    double numberOfPennies = remainderOfNickels/pennies; 










    /* 
    * Now you're ready to calculate the amount in USD. 
    */ 




    /* 
    * Determine what the transaction fee would be, based on the 
    * percentage. 
    */ 
    double totalTransactionFee = (userCurrencyInput*transactionFee); 
    System.out.println("The transcation fee for your currency exchange would be $" + totalTransactionFee + " US."); 


    /* 
    * If the transaction fee is less than the minimum transaction 
    * fee, you'll need to charge the minimum transaction fee. 
    * (Hint, the Math class has min and max methods that receive 
    * two numbers and return either the smaller or larger of those 
    * two numbers.) 
    */ 





    /* 
    * You'll need to deduct the transaction fee from the total. 
    */ 



    /* 
    * Calculate the number of $20's they'll receive 
    */ 




    /* 
    * How much is left? 
    */ 


    /* 
    * Next do the same for $10's, $5's, etc. 
    */ 
























    /* 
    * Finally, let the user know how many dollars their foreign currency 
    * converted to, what was deducted for the transaction fee, and how 
    * many of each denomination they are receiving. 
    */ 


    System.out.println("The amount of 20's is " +twentyDollarBills); 
    System.out.println("The amount of 10's is " +tenDollarBills); 
    System.out.println("The amount of 5's is " +fiveDollarBills); 
    System.out.println("The amount of 1's is " +oneDollarBills); 
    System.out.println("The amount of quarters is " +numberOfQuarters); 
    System.out.println("The amount of dimes is " +numberOfDimes); 
    System.out.println("The amount of nickels is " +numberOfNickels); 
    System.out.println("The amount of pennies is " +numberOfPennies); 






}//end main 

}//end Currency 
+0

如果您想處理貨幣,請使用'BigDecimal'。 – Laf 2014-09-10 19:38:02

+0

對貨幣使用雙倍請注意。雙數據類型沒有0.01或0.001的精確表示,或者對於這些數字的許多不同倍數。通常任何代表金錢數量的數字都應該是一個整數。例如,而不是5.33美元,你想要與533便士一起工作。使用double僅用於轉換率,並立即將結果轉換爲整數個便士(或其他)。 – 2014-09-10 19:40:11

+0

除了你應該使用'BigDecimal'或''long'這個事實,你還沒有具體告訴我們你的問題是什麼,只是你在掙扎,而且你有問題。這對我們提供幫助不夠好。 – ajb 2014-09-10 19:44:58

回答

0

首先,你不應該從double S轉換到int S,你會小數點後輸金額。例如

double num = 1.9; 
int number = (int) num; 
System.out.println(number); 

將打印

1 

此外,如果你做

System.out.println(num % 1); 

0.8999999999999999 // not quite 0.9, which is what you were expecting 

由於@Laf指出,使用BigDecimal代替。

BigDecimal SEK_CONVERSION_RATE = new BigDecimal(".14"); 
String userInput = keyboard.nextLine(); 
// Better precision constructing from a String 
BigDecimal userCurrencyInput = new BigDecimal(userInput); 

BigDecimal calculatedCurrInput = userCurrencyInput.multiply(SEK_CONVERSTION_RATE); 

希望你能從那裏找出其餘的。

提示:BigDecimal有一個方法divideAndRemainder()返回BigDecimal[],其中第一部分是商,第二部分是餘數。

http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#divideAndRemainder%28java.math.BigDecimal%29

+0

是否有你想使用int的原因? – Fodder 2014-09-11 03:25:32