Saturday, June 21, 2014

Java Exercise Nr. 45 (Selections)

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.

Solution:

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

Author