我正在创建一个银行帐户计划,我正在制作它以便余额总是以美元计算,但问题是我并不完全知道如何将这些方法结合在一起。这是我的一些代码。我想问的是转换器方法如何获取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;
}
}
}
}
答案 0 :(得分:2)
您可能应该将开关移到另一个方法,因为这是计算发生的地方。
您似乎走在正确的道路上,但需要提示转换金额。
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;
。
答案 1 :(得分:0)
这是我的代码。您可以尝试并告诉我它是否符合您的要求。
范围问题或重复变量。
你见面可能是因为你没有写回复陈述或&#34;打破&#34;如果任何开关盒匹配。
public class ConverTest {
public static double convertCurrency(double amount, int currencyType, boolean isConvertToUSD){
// checks what type of currency was chosen and converts each it into US dollars
System.out.println("the input type:" + currencyType); //you say you want to get the values used in my switch
// statement.
return 0.01; //i don't know what u want to do in this meshod.
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
convertCurrency(100.0, currencyMenuOptionSelector(in), true);
}
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;
return currencyType;
}
case 2: {
double Euro = 0.89;
return currencyType;
}
default: {
System.out.println("Input failed validation. Please try again.");
continue;
}
}
}
}
}