gpt4 book ai didi

java - 程序从 switch java 中断

转载 作者:行者123 更新时间:2023-12-01 17:00:41 24 4
gpt4 key购买 nike

由于某种原因,当我在切换菜单上输入“1”时,没有任何反应,但程序不会终止。与选项 2-5 相同。默认选项工作得很好。谁能帮我这个?谢谢

代码:

import java.util.Scanner;
public class ConversionTrial {
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;

Scanner input = new Scanner(System.in);
Scanner exchange = new Scanner(System.in);

menu: while(true) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");

choice = input.nextInt();

switch(choice){
case -1:
case 6:
break menu;
case 1:
pound = exchange.nextDouble();
System.out.print("Please enter your values you would like to exchange:");
break;
case 2:
pound = exchange.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are" + poundEuro);
case 3:
pound = exchange.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are" + poundDollars);
case 4:
pound = exchange.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are" + poundYen);
case 5:
pound = exchange.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are" + poundRupees);
default:
System.out.println("You must enter an option between 1 and 6!");

}
}
input.close();
exchange.close();
}
}

最佳答案

首先,不要创建两个扫描仪对象。只需创建一个并使用它即可。

其次,在选项 1-5 中,您在向用户输出任何内容之前等待输入,因此这可能就是它似乎不起作用的原因。您应该添加预期值的提示。

第三,在案例 2-5 的末尾缺少 break;

第四,使用标签通常不是最好的做事方式。它最终可能会产生一些难以阅读的代码。更好的方法是使用一个标志变量,boolean exit = false;。然后,您的 while 循环将基于它不为 true 进行循环,while(!exit)。在你的情况6:中,exit = true;

第五,当用户没有选择退出时,为什么会有 -1 退出?我会删除它。

import java.util.Scanner;
public class ConversionTrial{
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;

Scanner input = new Scanner(System.in);

boolean exit = false;

while(!exit) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");

choice = input.nextInt();

switch(choice){
case 6:
exit = true;
break;
case 1:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
break;
case 2:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are " + poundEuro);
break;
case 3:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are " + poundDollars);
break;
case 4:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are " + poundYen);
break;
case 5:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are " + poundRupees);
break;
default:
System.out.println("You must enter an option between 1 and 6!");
break;
}
}
input.close();
}
}

编辑:作为奖励,我还注意到选项 1 实际上没有执行任何操作。这是故意的吗?为了使代码更清晰,我会在定义变量时初始化转换变量的值,而不是每次使用它们时。您还可以在菜单中使用这些值,因此如果值发生变化,只需更改一次。

关于java - 程序从 switch java 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27840619/

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