The problem 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.
Breaking it down public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out . print ( "Enter the exchange rate from dollars to RMB: " );
double dollarToRMBRate = input . nextDouble ();
System . out
. print ( "Enter 0 to convert dollars to RMB and 1 vice versa: " );
int convertOption = input . nextInt ();
double dollars ;
double RMB ;
if ( convertOption == 0 ) {
System . out . print ( "Enter the dollar amount: " );
dollars = input . nextDouble ();
RMB = dollars * dollarToRMBRate ;
System . out . println ( "$" + dollars + " is " + RMB + " Yuan" );
} else if ( convertOption == 1 ) {
System . out . print ( "Enter the RMB amount: " );
RMB = input . nextDouble ();
dollars = RMB / dollarToRMBRate ;
System . out . println ( RMB + " Yuan" + " is " + "$" + dollars );
} else {
System . out . println ( "Incorrect input" );
}
input . close ();
}
Output Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 1
Enter the RMB amount: 10000
10000.0 Yuan is $1468.4287812041116
Financials currency exchange posted by Justin Musgrove on 09 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch3
Share on: Facebook Google+