2014-11-03 75 views
-1
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类和相应的文本文件以在屏幕上打印。在此之后,用户将被提示更新文件,或不用下一次迭代。我不确定为什么这不按照我喜欢的方式工作,我认为我的推理和逻辑是合理的。在switch语句中可能有多个值吗?

+0

'one ||两个||三||四||五'是一个无效的表达。你在这个switch语句中想做什么? – Eran 2014-11-03 14:30:56

回答

2

我很确定你想根据值初始化你的gol变量(并且你并不真的需要一个or,并且y我们的开关情况下需要是常量)像

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); 
} 
4

使用秋天,虽然情况

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

请仔细阅读The switch Statement作为一般的指南,这个格式构建

+0

感谢您输入Reimeus,我一直在这个问题上工作了几个小时,而我没有看清楚它。 – Wes 2014-11-03 14:35:12

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: 
    } 
1

INT一,二,三,四,五;

首先制作一个变量,您将在其中存储您的值。

事情是这样的:

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; 
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. 
} 
1

它的更好,如果你要求用户输入一个选择,然后切换选择变量。 U也可以使用开关结构在所有情况下创建对象,或者使用通过开关结构(不要写入中断)。