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
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:
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: 1 Enter the yuan amount: 1000 1000.0 yuan is $146.843 dollar 146 dollars 3 quarters 0 dimes 1 nickel 4 pennies
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:
146 dollars 3 quarters 0 dimes 1 nickel 4 pennies
which have to use indexOf() and substring() methods.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started