Как вы можете видеть, в приведенном ниже коде есть много операторов 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.");
да, 'переключатель (игроков) {случай 2: ....; break ... ' –