switch语句中是否可以有多个值?

时间:2014-11-03 14:28:54

标签: java

    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Main {

        public static void main(String[] args) throws FileNotFoundException {

            Scanner kb = new Scanner(System.in);
            System.out.println("Which file would you like to test?");
            System.out.println();
            System.out.println("Enter '1' for life1");
            System.out.println("Enter '2' for life2");
            System.out.println("Enter '3' for life3");
            System.out.println("Enter '4' for life4");
            System.out.println("Enter '5' for life5");
            kb.nextInt();
            int one, two, three, four, five;
            one = 1; two = 2; three = 3; four = 4; five = 5;
            switch(one || two || three || four || five){
            case 1:{
                GameOfLife gol = new GameOfLife("Input/life1.txt");
                gol.print("Input/life1.txt");
                break;
            }
        }
    }
}

以下是我正在使用的代码示例。基本上我想要做的是评估用户从键盘输入的内容,然后实例化GameOfLife类和相应的文本文件以在屏幕上打印。在此之后,然后将提示用户在下一次迭代时更新文件。我不确定为什么这不符合我喜欢的方式,我认为我的推理和逻辑是合理的。

6 个答案:

答案 0 :(得分:4)

使用fall-though case

int value = keyboard.nextInt();
switch (value) {
      case ONE:
      case TWO:
      case THREE:
      ...
}

请阅读The switch Statement作为此构造格式的一般指南

答案 1 :(得分:3)

Swith允许连续放几个案例,但不在交换机中:

    final int a = 5;
    switch (a) {
    case 1:
    case 2:
    case 3:
        // do something
        break;
    case 4:
    case 5:
        // do something
        break;
    default:
    }

答案 2 :(得分:2)

我很确定你想根据值初始化你的gol变量(并且你真的不需要一个或者,你的开关案例需要是常量),比如

GameOfLife gol = null;
final int one = 1, two = 2, three = 3, four = 4, five = 5;
switch (test) {
case one:
    gol = new GameOfLife("Input/life1.txt");
    gol.print("Input/life1.txt");
    break;
case two:
    gol = new GameOfLife("Input/life2.txt");
    gol.print("Input/life2.txt");
    break;
case three:
    gol = new GameOfLife("Input/life3.txt");
    gol.print("Input/life3.txt");
    break;
case four:
    gol = new GameOfLife("Input/life4.txt");
    gol.print("Input/life4.txt");
    break;
case five:
    gol = new GameOfLife("Input/life5.txt");
    gol.print("Input/life5.txt");
    break;
}

但是,我认为这应该看起来像

GameOfLife gol = null;
final int one = 1, two = 2, three = 3, four = 4, five = 5;
switch (test) {
case one:
    gol = new GameOfLife("Input/life1.txt");
    break;
case two:
    gol = new GameOfLife("Input/life2.txt");
    break;
case three:
    gol = new GameOfLife("Input/life3.txt");
    break;
case four:
    gol = new GameOfLife("Input/life4.txt");
    break;
case five:
    gol = new GameOfLife("Input/life5.txt");
    break;
}
if (gol != null) {
  gol.print();
} else {
  System.err.println("error: no such GameOfLife " + test);
}   

答案 3 :(得分:1)

int one,two,three,four,five;

首先制作一个变量,存储你的值。

这样的事情:

int value;

让我们说这个值可以从1-5开始。

int value = 2;

现在,根据价值,我们不会对它做点什么:

switch(value){

    case 1:
            GameOfLife gol = new GameOfLife("Input/life1.txt");
            gol.print("Input/life1.txt");
    break;

    case 2:
            GameOfLife gol = new GameOfLife("Input/life2.txt");
            gol.print("Input/life2.txt");
    break;

    ...

    case 5:
            GameOfLife gol = new GameOfLife("Input/life5.txt");
            gol.print("Input/life5.txt");
    break;
        }

如果之前没有任何内容属实,你也可以添加案例......如果不知何故,你的价值是> 5

default: 
   //do something
break;

答案 4 :(得分:1)

如果您要求用户输入选项然后切换选择变量,那就更好了。 U也可以使用switch构造然后在所有情况下创建对象或使用fall through switch构造(不要写中断)。

答案 5 :(得分:0)

您似乎不需要那么多变量,只需要接收用户输入的变量。正如@Reimeus所说,你在switch()sintax中使用那个单一变量,并查看该变量的各种可能情况。 所以:

int value = kb.nextInt() (I dont know this Scanner class, I just assume this is correct)
switch(value){
 case 1:
 //do something
 break;
 case 2:
//do something
break;
 case 3:
 case 4:
   //do something - in this case, both cases value=3 and value=4 will respond equally, until a BREAK command if found;
   break;
 default:
  //do something in the case that tha value of "value" did not fall into any of the previous cases.
}