2014-11-03 77 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也可以使用開關結構在所有情況下創建對象,或者使用通過開關結構(不要寫入中斷)。