2017-01-18 171 views
-1

正如您所看到的,下面的代碼中有很多if語句。我可以在此代碼中使用case語句而不是if語句嗎?

我可以用case語句替換它們嗎?

package CBM; 

import java.util.Scanner; 
import java.util.Random; 

public class Board 
{ 
    public static void main(String args[]) 
    { 
    int minplayers, maxplayers; 
    minplayers = 2; 
    maxplayers = 8; 
    String player_0, player_1; 

    System.out.println("Start game? (Y/N)"); 
    Scanner sc = new Scanner(System.in); 
    String yn = sc.next(); 
    if (yn.equals("Y")) 
     System.out.println("Starting"); 
     else 
     { 
      if (yn.equals("N")) 
       System.out.println("Ending"); 
     } 
    System.out.println("How many people are playing?"); 
    int players = sc.nextInt(); 
    if (players >= minplayers && players <= maxplayers) 
     System.out.println("There are " + players + " players"); 
     else 
     { 
      if (players < minplayers || players > maxplayers) 
       System.out.println("2-8 players only."); 
       System.out.println("Ending"); 
     } 

    if (players == 2) 
     System.out.println("To begin both players must each roll the dice once" 
       + " to decide who goes first."); 

    else 

    if (players == 3) 
     System.out.println("To begin all 3 players must each roll the dice once" 
       + " to decide who goes first."); 
    else 

    if (players == 4) 
     System.out.println("To begin all 4 players must each roll the dice once" 
       + " to decide who goes first."); 
    else 

    if (players == 5) 
     System.out.println("To begin all 5 players must each roll the dice once" 
       + " to decide who goes first."); 
    else 

    if (players == 6) 
     System.out.println("To begin all 6 players must each roll the dice once" 
       + " to decide who goes first."); 
    else 

    if (players == 7) 
     System.out.println("To begin all 7 players must each roll the dice once" 
       + " to decide who goes first."); 
    else 

    if (players == 8) 
     System.out.println("To begin all 8 players must each roll the dice once" 
       + " to decide who goes first."); 
+0

是的,'切換(球員){情況2:....;打破...' –

回答

0

你不需要ifswitch。只需插入播放動態計數(與第一種情況例外):

if (players == 2) 
    System.out.println("To begin both players must each roll the dice once" 
      + " to decide who goes first."); 
else 
    System.out.println("To begin all " + players + " players must each roll the dice once" 
      + " to decide who goes first."); 
+0

這是一個非常簡單的方法,謝謝! – user7431342

0

你只需要檢查players == 2因爲你要打印出來的字,在所有其他情況下,你可以只打印在變量players之後,因爲您已經在代碼的早些時候檢查了它的正確範圍。

if (players == 2) 
    System.out.println("To begin both players must each roll the dice once to decide who goes first."); 
else 
    System.out.println("To begin all " + players + " players must each roll the dice once to decide who goes first."); 

如果你想使用switch語句你這樣做:

switch (players) { 
    case 2: 
    System.out.println("To begin both players must each roll the dice once to decide who goes first."); 
    break; 
    default: 
    System.out.println("To begin all " + players + " players must each roll the dice once to decide who goes first."); 
    break; 
} 
+0

謝謝你的回覆:) – user7431342

0

你應該考慮檢查時players == 2players > 2刪除許多不必要的情況下。 無論如何,如果你想使用的情況下,它將是:

switch(players) 
{ 
case 2: 
//your code 
break; 
case 3: 
//your code 
break; 
... 
} 
+0

好吧,謝謝你的反饋。 – user7431342

相關問題