Java、货币兑换

        编写一个程序,提示用户输入从美元到人民币的兑换汇率。提示用户输入 0 表示从美元兑换为人民币,输入 1 表示从人民币兑换为美元。继而提示用户输入美元数量或者人民币数量,分别兑换为另外一种货币。


package pack2;

import java.util.Scanner;

public class ExchangeRate {

	public static void main(String[] args) {
		try(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 tag = input.nextInt();
			
			if(tag != 0 && tag != 1) {	//判定输入是否为 0 或 1
				System.out.println("Incorrect input");
				return;
			}
			
			System.out.print("Enter the "+(tag == 0 ? "dollars" : "RMB")+" amount: ");
			double amount = input.nextDouble();
			
			if(tag == 0)	//tag == 0 时表示美元到人民币
				System.out.println("$"+amount+" is "+(amount * rate)+" yuan");
			else			//否则表示人民币到美元
				System.out.printf("%.1f yuan is $%.2f\n", amount, (amount / rate));
		}
	}

}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值