我可以在此代码中使用case语句而不是if语句吗?

时间:2017-01-18 08:09:41

标签: java if-statement switch-statement 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.");

3 个答案:

答案 0 :(得分: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.");

答案 1 :(得分:0)

您只需要检查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 :(得分:0)

您应该考虑在players == 2players > 2何时删除许多不必要的案例。 无论如何,如果你想使用case语句,它将是:

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