2017-01-18 7 views
-1

Как вы можете видеть, в приведенном ниже коде есть много операторов if.Могу ли я использовать оператор case вместо инструкции if в этом коде?

Могу ли я заменить их на регистр случаев?

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: ....; break ... ' –

ответ

0

Вам не нужно ifилиswitch. Просто вставьте подсчитывать игрок динамически (с исключением для первого случая):

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 (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 == 2 и когда players > 2 удалить много ненужных дел. Во всяком случае, если вы хотите использовать случае заявление будет:

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

Правильно, спасибо за отзыв. – user7431342

 Смежные вопросы

  • Нет связанных вопросов^_^