gpt4 book ai didi

java - 我无法将这两种方法联系在一起

转载 作者:行者123 更新时间:2023-12-02 03:16:23 25 4
gpt4 key购买 nike

我正在创建一个银行帐户程序,并且我正在制作它,以便余额始终以美元为单位,但问题是我不完全知道如何将这些方法结合在一起。这是我的一些代码。我想问的是转换器方法如何获取 switch 语句中使用的值?我尝试了多种不同的方法,但总是遇到范围问题或重复的变量。

public static double convertCurrency(double amount, int currencyType, boolean isConvertToUSD){
// checks what type of currency was chosen and converts each it into US dollars

}
}

public static int currencyMenuOptionSelector(Scanner console){
while(true){
System.out.println("Please select the currency type:");
System.out.println("1. U.S. Dollars");
System.out.println("2. Euros");
System.out.println("3. British Pounds");
System.out.println("4. Indian Rupees");
System.out.println("5. Australian Dollars");
System.out.println("6. Canadian Dollars");
System.out.println("7. Singapore Dollars");
System.out.println("8. Swiss Francs");
System.out.println("9. Malaysian Ringgits");
System.out.println("10. Japanese Yen");
System.out.println("11. Chinese Yuan Renminbi");
int currencyType = console.nextInt();

switch (currencyType){
// cases 1-11 simply establishes each currency as a variable (not all cases are listed to avoid redundancy)
case 1: {
double USA = 1.00;
}
case 2: {
double Euro = 0.89;

default: {
System.out.println("Input failed validation. Please try again.");
continue;
}
}


}

}

最佳答案

您可能应该将开关移至其他方法,因为这是进行计算的地方。

您似乎走在正确的道路上,但需要提示输入要转换的金额。

System.out.print("Enter the type of currency: ");
int currencyType = Integer.parseInt(console.nextLine());

System.out.print("Enter currency amount: ");
double amount = Double.parseDouble(console.nextLine());

if (currencyType < 1 || currencyType > 11) {
// TODO: Handle invalid input.
}

boolean toUSD = currencyType == 1;
double convertedAmount = convertCurrency(amount, currencyType, toUSD);

并且您的案例中需要 break;

关于java - 我无法将这两种方法联系在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com