LabTask week06 Write a program that prompts the user to enter the exchange rate
ID: 3607487 • Letter: L
Question
LabTask week06
Write a program that prompts the user to enter the exchange rate from currency in US dollars to Chinese RMB. Prompt the user to enter 0 to convert from US dollars to Chinese RMB and 1 to convert from Chinese RMB to US dollars. Prompt the user to enter the amount in US dollars or Chinese RMB to convert it to Chinese RMB or US dollars, respectively. When the user enters "1" (yuans to dollars) the program should extract the dollar amount before the decimal point, and the cents after the decimal amount using the indexOf() and substring() methods.Here are the sample runs, user's input is underlined:
I already have some code, which is:
import java.util.Scanner;
public class abc
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the exchange rate from dollars to RMB: ");
double rate = input.nextDouble();
System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
int option = input.nextInt();
double amount;
switch(option)
{
case 0: System.out.print("Enter the dollar amount: ");
amount = input.nextDouble();
System.out.println("$" + amount + " is " +
(amount*rate) + " yuan"); break;
case 1: System.out.print("Enter the RMB amount: ");
amount = input.nextDouble();
System.out.println(amount + " yuan is $" +
((int)((amount*100)/rate))/100.0); break;
default: System.out.println("Incorrect input");
}
}
}
But I don't know how to show the list:
which have to use indexOf() and substring() methods.
Explanation / Answer
abc.java
import java.util.Scanner;
public class abc
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the exchange rate from dollars to RMB: ");
double rate = input.nextDouble();
System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
int option = input.nextInt();
double amount;
switch(option)
{
case 0: System.out.print("Enter the dollar amount: ");
amount = input.nextDouble();
System.out.println("$" + amount + " is " +
(amount*rate) + " yuan"); break;
case 1: System.out.print("Enter the RMB amount: ");
amount = input.nextDouble();
double yuan = ((int)((amount*100)/rate))/100.0;
System.out.println(amount + " yuan is $" + yuan);
System.out.println((int)yuan+" dollars");
int change = (int)(yuan * 100 ) % 100;
System.out.println(change/25+" quarters");
change = change % 25;
System.out.println(change/10+" dimes");
change = change % 10;
System.out.println(change/5+" nickel");
change = change % 5;
System.out.println(change+" pennies");
break;
default: System.out.println("Incorrect input");
}
}
}
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: 1000
1000.0 yuan is $146.84
146 dollars
3 quarters
0 dimes
1 nickel
4 pennies
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.