如何将 if-else 语句转换为 switch 语句

How do i convert an if-else statement to a switch statement

我有这个程序,可以掷 1000000 个骰子并找出 1、2、3、4、5、6 的个数,但我还需要做一个 switch 语句。我在使用扫描仪输入时遇到问题

import java.util.Random;

public class DiceRoll_NP {

    public static void main(String[] args) {
        Random rand = new Random();
        final double NUMBER_OF_ROLLS = 1000000.0;

        int x = rand.nextInt();
        // System.out.println(x);

        int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;

        for (int i = 1; i <= NUMBER_OF_ROLLS; i = i + 1) {
            int y = rand.nextInt(6) + 1;
            if (y == 1) ones++;
            else if (y == 2) twos++;
            else if (y == 3) threes++;
            else if (y == 4) fours++;
            else if (y == 5) fives++;
            else if (y == 6) sixes++;
            System.out.print(y + " ");
        }
        System.out.printf("\nOnes: %.2f%%\n", 100 * ones / NUMBER_OF_ROLLS);
        System.out.printf("Twos: %.2f%%\n", 100 * twos / NUMBER_OF_ROLLS);
        System.out.printf("Threes: %.2f%%\n", 100 * threes / NUMBER_OF_ROLLS);
        System.out.printf("Fours: %.2f%%\n", 100 * fours / NUMBER_OF_ROLLS);
        System.out.printf("Fives: %.2f%%\n", 100 * fives / NUMBER_OF_ROLLS);
        System.out.printf("sixes: %.2f%%\n", 100 * sixes / NUMBER_OF_ROLLS);
    }

}

这是我目前所拥有的。当我 运行 它时,什么也没有出现

导入java.util.Random;

public class DiceRoll_Switch {

public static void main(String[] args) {
    Random rand = new Random();
    int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;
    int y = rand.nextInt(6) + 1;
    for (int i = 1; i <= 1000000; i = i + 1)
        switch (y){
        case 1: ones++;
        break;
        case 2: twos++;
        break;
        case 3: threes++;
        break;
        case 4: fours++;
        break;
        case 5: fives++;
        break;
        case 6: sixes++;
        break;

非常相似。这两个几乎在做完全相同的事情:

int x = 2;
if(x == 1) {
    System.out.println("1");
}
else if(x == 2) {
    System.out.println("2");
}
else {
    System.out.println("None of the above");
}

int x = 2;
switch(x) {
    case 1:
        System.out.println("1");
        break;
    case 2:
        System.out.println("2");
        break;
    default:
        System.out.println("None of the above");
        break;
}

在编程的早期,goto 语句被大量使用。它是这样工作的:

if ( true )
   goto label;

System.out.println("A");

label:

System.out.println("B");

上面的代码会跳转到标签,跳过打印 A。这很简单。

现在,我为什么要提到这个?下面是一个 if 语句的例子:

if ( y == 1 )
  System.out.println("It is 1");
else
  System.out.println("It is not 1");

这是使用 goto:

的工作原理
if ( y != 1 )
   goto not_1;

// this is the 'then' part:
System.out.println("It is 1");
goto done;   // we don't want to run the 'else' part!

not_1:
// this is the 'else' part
System.out.println("It is not 1");

done:

如果你明白这一点,那么一个switch语句就不会有任何问题。 switch 语句如下所示:

switch (y) { 
  case 1: ones++; break;
  case 2: tows++; break;
  default: System.out.println("Unexpected number of eyes: " + y);
}

它将 y 的值与 case 之后的所有值进行比较,如果匹配,则开始执行 case 处的代码。 break 表示 case 已完成。 default 'case' 在 case 的 none 匹配时执行。

这是使用 goto:

的基本工作原理
if ( y == 1 ) goto case_1;
if ( y == 2 ) goto case_2;

// here comes the default:
default:
System.out.println("Unexpected number of eyes: " + y);
goto switch_done;

case_1:
  ones++;
  goto switch_done; // this is the 'break' statement.

case_2:
  twos++;
  goto switch_done; 

switch_done:  // end of the switch.  

我希望这能让大家更清楚地了解它是如何工作的!

也就是说,您最好使用数组:

int eyes[] = new int[] { 0,0,0,0,0,0 };

for ( int i = 1; i <= NUMBER_OF_ROLLS; i++ )
{
   int y = rand.nextInt(6) + 1;
   eyes[y] ++;
}

这也使您的打印更短:

for ( int i = 0; i < eyes.length; i ++ )
   System.out.printf( (i+1) + ": %.2f%%\n", 100 * eyes[i] / NUMBER_OF_ROLLS );

你基本上对每个 y 做同样的事情,你应该想写尽可能少的代码:它更容易维护和理解,而且出现错误的机会更少。

一个 switch 语句包括:

switch (y) {
  case 1:
    ones++;
    break;
  case 2:
    twos++;
    break;
...
}

等,其中括号内的值为要检查的变量名,"case"后的数字为可能发生的情况。你必须在你的指令结束后休息一下,告诉程序它可以在找到正确的语句并停止执行后退出 switch 语句。否则,如果你不休息; "case 1" 块中的命令,它将继续执行每一行,直到找到中断,并且 "ones" 和 "twos" 都将被添加