*3.31 (Financials: currency exchange) Write a program that prompts the user to enter
the exchange rate from currency in U.S. dollars to Chinese RMB. Prompt the user
to enter 0 to convert from U.S. dollars to Chinese RMB and 1 to convert from
Chinese RMB and U.S. dollars. Prompt the user to enter the amount in U.S. dollars
or Chinese RMB to convert it to Chinese RMB or U.S. dollars, respectively.
Here are the sample runs:
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 0 Enter the dollar amount: 100 $100.0 is 681.0 yuan
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Enter the RMB amount: 10000 10000.0 yuan is $1468.43
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Incorrect input
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 0 Enter the dollar amount: 100 $100.0 is 681.0 yuan
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Enter the RMB amount: 10000 10000.0 yuan is $1468.43
Enter the exchange rate from dollars to RMB: 6.81 Enter 0 to convert dollars to RMB and 1 vice versa: 5 Incorrect input
public static void main(String[] args) { Scanner s = new Scanner(System.in); double exchangeRate, dollarAmount, rmbAmount; int userChoice; System.out.print("Enter the exchange rate from dollars to RMB:"); exchangeRate = s.nextDouble(); System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa:"); userChoice = s.nextInt(); if(userChoice == 0) { System.out.print("Enter the dollar amount:"); dollarAmount = s.nextDouble(); rmbAmount = dollarAmount * exchangeRate; System.out.print("$" + dollarAmount + " is " + rmbAmount + " yuan."); } else if(userChoice == 1) { System.out.print("Enter the RMB amount:"); rmbAmount = s.nextDouble(); dollarAmount = rmbAmount/exchangeRate; System.out.print(rmbAmount + " yuan is $" + dollarAmount); } else { System.out.print("Invalid choice."); } }
No comments :
Post a Comment