正如您所看到的,下面的代碼中有很多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.");
是的,'切換(球員){情況2:....;打破...' –