*3.31(金融:货币兑换)编写一个程序,提示用户输入从美元到人民币的兑换汇率。然后提示用户输入0表示从美元兑换为人民币,输入1表示从人民币兑换为美元。继而提示用户输入美元数量或者人民币数量,分别兑换为另外一种货币。
下面是运行示例:
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 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
Incorrent input
*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 to 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:1
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
Incorrent input
下面是参考答案代码:
import java.util.*;
public class CurrencyExchangeQuestion31 {
public static void main(String[] args) {
double exchangeRate,moneyAmount,exchangedAmount;
final int exchangeMode;
System.out.print("Enter the exchange rate from dollars to RMB: ");
Scanner input = new Scanner(System.in);
exchangeRate = input.nextDouble();
System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
exchangeMode = input.nextInt();
if(exchangeMode == 0)
{
System.out.print("Enter the dollar amount: ");
moneyAmount = input.nextDouble();
exchangedAmount = moneyAmount * exchangeRate;
System.out.println("$" + moneyAmount + " is " + exchangedAmount + " yuan");
}
else if(exchangeMode == 1)
{
System.out.print("Enter the RMB amount: ");
moneyAmount = input.nextDouble();
exchangedAmount = moneyAmount / exchangeRate;
System.out.println(moneyAmount + " yuan is $" + exchangedAmount);
}
else
System.out.println("Incorrect input");
input.close();
}
}
运行效果:
注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)