来自switch java的程序中断

时间:2015-01-08 12:52:23

标签: java

出于某种原因,当我进入' 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();
}
}

6 个答案:

答案 0 :(得分:4)

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

其次,在选项1-5中,您在向用户输出任何内容之前等待输入,这可能是为什么它似乎无法正常工作。您应该为期望值添加提示。

第三,在案例2-5结束时你遗漏了break;

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

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

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 :(得分:1)

menu标签确实是必要的。摆脱它,这是不好的代码味道。

此外,在所有其他情况下,您都会错过break;

答案 2 :(得分:0)

为每个案例添加中断以退出switch语句。不确定这是否是你想要的答案,所以如果你正在寻找别的东西,请告诉我。

答案 3 :(得分:0)

就像PeterMmm说的那样。您忘记了案件中的break;。 你的代码正在运行,但我认为它不是你想要它做的。 如果我按1我可以输入一个双,然后它告诉我我可以输入一个值。 但是,来自开关1的这个值恰好没有发生。 其他选项我可以输入双倍货币并以外币进行兑换。

你打算做什么?

答案 4 :(得分:0)

只是运行你的代码,我已经弄明白你想做什么。我得到以下内容:

> 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来为交换机中存在的每个案例退出交换机。

此致

答案 5 :(得分:0)

当您选择选项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();
}
}