Java 在这段代码中,我可以使用case语句而不是if语句吗?

Java 在这段代码中,我可以使用case语句而不是if语句吗?,java,if-statement,switch-statement,case,Java,If Statement,Switch Statement,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;

正如您所看到的,在下面的代码中有许多
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.");
包CBM;
导入java.util.Scanner;
导入java.util.Random;
公共班级委员会
{
公共静态void main(字符串参数[])
{
int minplayers,maxplayers;
minplayers=2;
maxplayers=8;
弦乐演奏者0,演奏者1;
System.out.println(“开始游戏(Y/N)”);
扫描仪sc=新的扫描仪(System.in);
字符串yn=sc.next();
如果(yn等于(“Y”))
System.out.println(“启动”);
其他的
{
如果(yn等于(“N”))
系统输出打印项次(“结束”);
}
有多少人在玩;
int players=sc.nextInt();
if(players>=minplayers&&players-maxplayers)
System.out.println(“仅限2-8名玩家”);
系统输出打印项次(“结束”);
}
如果(玩家==2)
System.out.println(“两名玩家必须每人掷一次骰子才能开始”
+“决定谁先去。”);
其他的
如果(玩家==3)
System.out.println(“开始时,所有3名玩家必须每人掷骰子一次”
+“决定谁先去。”);
其他的
如果(玩家==4)
System.out.println(“要开始,所有4名玩家必须每人掷骰子一次”
+“决定谁先去。”);
其他的
如果(玩家==5)
System.out.println(“开始时,所有5名玩家必须每人掷骰子一次”
+“决定谁先去。”);
其他的
如果(玩家==6)
System.out.println(“开始时,所有6名玩家必须每人掷骰子一次”
+“决定谁先去。”);
其他的
如果(玩家==7)
System.out.println(“开始时,所有7名玩家必须每人掷骰子一次”
+“决定谁先去。”);
其他的
如果(玩家==8)
System.out.println(“开始时,所有8名玩家必须每人掷骰子一次”
+“决定谁先去。”);

如果或
开关
不需要
。只需动态插入玩家计数(第一种情况除外):


您只需要检查
players==2
,因为您想打印出单词,在所有其他情况下,您可以只打印出变量
players
,因为您已经检查了它在代码前面的正确范围
(2-8)

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;
}

你应该考虑检查<代码>播放器=2 以及当<代码>玩家> 2 > /代码>删除许多不必要的情况。 无论如何,如果您想使用用例语句,它将是:

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

是的,
switch(players){案例二:…;break…
谢谢你的回复:)
switch(players)
{
 case 2:
 //your code
 break;
 case 3:
 //your code
 break;
 ...
 }