第三章第三十一题(金融:货币兑换)(Financials: currency exchange)

第三章第三十一题(金融:货币兑换)(Financials: currency exchange)

  • *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

  • 参考代码:

package chapter03;

import java.util.Scanner;

public class Code_31 {
    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();
    }
}

  • 结果显示:
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

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值