【问题标题】:Can I use a case statement instead of an if statement in this code?我可以在这段代码中使用 case 语句而不是 if 语句吗?
【发布时间】:2017-01-18 08:09:40
【问题描述】:

如您所见,下面的代码中有很多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.");

【问题讨论】:

  • 是的,switch (players) {case 2: ....; break ...

标签: java if-statement switch-statement case


【解决方案1】:

你不需要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.");

【讨论】:

    【解决方案2】:

    您只需要检查players == 2,因为您想打印出both这个词,在所有其他情况下,您只需打印出变量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;
    }
    

    【讨论】:

    • 感谢您的回复 :)
    【解决方案3】:

    您应该考虑检查何时players == 2 和何时players &gt; 2 以删除许多不必要的情况。 无论如何,如果您想使用 case 语句,它将是:

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

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2017-05-07
      • 1970-01-01
      • 2021-03-02
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多