程序从java开关中断

程序从java开关中断,java,Java,由于某种原因,当我在switch(切换)菜单上输入'1'时,不会发生任何事情,但程序不会终止。选项2-5也一样。默认选项工作正常。有人能帮我吗?谢谢 代码: import java.util.Scanner; public class ConversionTrial { public static void main(String[] args) { double pound; double euro; double dollars; double yen;

由于某种原因,当我在switch(切换)菜单上输入'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();
}
}

菜单
标签其实并不必要。摆脱它,这是糟糕的代码气味


此外,您还错过了
中断在所有其他情况下。

实际上不需要
菜单标签。摆脱它,这是糟糕的代码气味


此外,您还错过了
中断在所有其他情况下。

为每种情况添加中断以退出switch语句。不确定这是否是您想要的答案,因此如果您正在寻找其他答案,请告诉我。

为每个案例添加中断以退出switch语句。不确定这是否是您想要的答案,因此如果您正在寻找其他内容,请告诉我。

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

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

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

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

第五,当用户不能选择-1退出时,为什么要选择-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实际上什么都不做。这是故意的吗?对于更干净的代码,我会在定义变量时初始化转换变量的值,而不是每次使用它们。您也可以在菜单中使用这些值,因此,如果值发生更改,只需更改一次即可。

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

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

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

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

第五,当用户不能选择-1退出时,为什么要选择-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实际上什么都不做。这是故意的吗?对于更干净的代码,我会在定义变量时初始化转换变量的值,而不是每次使用它们。您也可以在菜单中使用这些值,因此,如果值发生变化,它们只需更改一次。

就像Petermm所说的那样。你忘记了
中断在您的案例中。
你的代码正在工作,但我认为它不是你想要它做的。
如果我按1,我可以输入一个double,然后它告诉我可以输入一个值。
但是开关1的这个值不会发生任何变化。
其他选项我可以输入一个double,然后用外币兑换


你打算做什么?

就像彼得姆说的那样。你忘记了
中断在您的案例中。
你的代码正在工作,但我认为它不是你想要它做的。
如果我按1,我可以输入一个double,然后它告诉我可以输入一个值。
但是开关1的这个值不会发生任何变化。
其他选项我可以输入一个double,然后用外币兑换


你打算做什么?

刚刚运行了你的代码,我已经知道你想做什么了。我得到以下信息:

> Please enter your values you would like to exchange:Please choose an
> option:
> 1.  Enter values
> 2.  Euros (1GBP = 1.28EUR)
> 3.  Dollars (1GBP = 1.51USD)
> 4.  Yen (1GBP = 179.80JPY)
> 5.  Rupees (1GBP = 95.60INR)
> 6.  Exit 2
> 1.2 Your amounts in Euros are1.536
但请注意,必须输入双值。 此外,我坚持使用break退出交换机中存在的每种情况


“问候”

刚刚运行了你的代码,我已经知道你想做什么了。我得到以下信息:

> Please enter your values you would like to exchange:Please choose an
> option:
> 1.  Enter values
> 2.  Euros (1GBP = 1.28EUR)
> 3.  Dollars (1GBP = 1.51USD)
> 4.  Yen (1GBP = 179.80JPY)
> 5.  Rupees (1GBP = 95.60INR)
> 6.  Exit 2
> 1.2 Your amounts in Euros are1.536
但请注意,必须输入双值。 此外,我坚持使用break退出交换机中存在的每种情况


关于

当您选择选项1-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:
                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);
                break;
            case 3:
                pound = exchange.nextDouble();
                dollars = 1.51;
                poundDollars = pound * dollars; 
                System.out.println("Your amounts in Dollars are" + poundDollars);
                break;
            case 4:
                pound = exchange.nextDouble();
                yen = 1.28;
                poundYen = pound * yen; 
                System.out.println("Your amounts in Yen are" + poundYen);
                break;
            case 5:
                pound = exchange.nextDouble();
                rupees = 1.28;
                poundRupees = pound * rupees; 
                System.out.println("Your amounts in Rupees are" + poundRupees);
                break;
            case -1:
            case 6:
                break menu;
            default:
                System.out.println("You must enter an option between 1 and 6!");

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

当您选择选项1-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:
                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);
                break;
            case 3:
                pound = exchange.nextDouble();
                dollars = 1.51;
                poundDollars = pound * dollars; 
                System.out.println("Your amounts in Dollars are" + poundDollars);
                break;
            case 4:
                pound = exchange.nextDouble();
                yen = 1.28;
                poundYen = pound * yen; 
                System.out.println("Your amounts in Yen are" + poundYen);
                break;
            case 5:
                pound = exchange.nextDouble();
                rupees = 1.28;
                poundRupees = pound * rupees; 
                System.out.println("Your amounts in Rupees are" + poundRupees);
                break;
            case -1:
            case 6:
                break menu;
            default:
                System.out.println("You must enter an option between 1 and 6!");

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

可能是因为您调用了
nextDouble
,所以它在等待一个双精度扫描仪?不要在System.in上创建两个扫描仪-只需创建一个并在任何地方使用它。可能是因为您调用了
nextDouble
,所以它在等待一个双精度扫描仪?不要在System.in上创建两个扫描仪-只需创建一个并在任何地方使用它。