Java货币转换器(如何将int转换为Double +许多其他问题)

时间:2014-09-10 19:32:15

标签: java currency

在转换货币然后将其除以美元金额时,我正在努力寻找剩余货币。此外,创建要转换的最小货币量也是一个问题。我理解我的代码很多,但这是我的第一个Java项目。任何帮助将非常感激。

 /* 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

1 个答案:

答案 0 :(得分:0)

首先,您不应该从double转换为int s,因为您将丢失小数点后的数量。 e.g。

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