Java黑皮书课后题第3章:*3.31(金融:货币兑换)编写程序,提示用户输入从美元到人民币的兑换汇率,然后提示用户输入0表美元兑人民币、1表人民币兑美元。继而提示用户输入美元数量或人民币数量,兑换
*3.31(金融:貨幣兌換)編寫程序,提示用戶輸入從美元到人民幣的兌換匯率,然后提示用戶輸入0表美元兌人民幣、1表人民幣兌美元。繼而提示用戶輸入美元數量或人民幣數量,兌換為另一種貨幣
- 題目
- 題目概述
- 運行示例
- 破題
- 代碼
題目
題目概述
*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 dollar 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
Incorrect input
破題
首先是要確定從美元到人民幣,還是從人民幣到美元,需要對應不同轉換方式
另外需要對輸入的數字進行合法性判斷
代碼
import java.util.Scanner;public class Test3_31 {public static void main(String[] args) {// 獲取用戶輸入數據Scanner input = new Scanner(System.in);System.out.println("Enter the exchange rate from dollars to RMB: ");double rate = input.nextDouble();System.out.println("Enter 0 to convert dollars to RMB and 1 vice versa: ");final int CHANGEWAY = input.nextInt();// 合法性判斷if(rate <= 0 || (CHANGEWAY != 0 && CHANGEWAY != 1)){System.out.println("Incorrect input");System.exit(1);}// 獲取需要轉換的貨幣數量System.out.println("Enter the dollar amount: ");double amount = input.nextDouble();// 轉換并輸出if(CHANGEWAY == 0){ // 美元兌人民幣System.out.println("$" + amount + " is " + amount * rate + " yuan");}elseSystem.out.println(amount + " yuan is $" + amount / rate);} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:*3.31(金融:货币兑换)编写程序,提示用户输入从美元到人民币的兑换汇率,然后提示用户输入0表美元兑人民币、1表人民币兑美元。继而提示用户输入美元数量或人民币数量,兑换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让PPT更好看的方法,需要的朋友快来吧
- 下一篇: 微信小程序一键置顶操作详解: