【问题标题】:How do I reset my values in a switch statement?如何在 switch 语句中重置我的值?
【发布时间】:2019-05-06 10:26:37
【问题描述】:

我正在开发一个使用随机数生成器的堆叠箱游戏,但每当一个数字被使用两次时,我都无法让我的程序重置?

1) 选择在创建盒子时跟踪盒子或直接找到解决方案的实现。 2) while 循环控制接下来的三个步骤。 3) 选择一个盒子 4) 判断这个盒子是否可以放置
5) 放置一个盒子
我使用了 6 个布尔变量,每个变量代表一个框。一个 switch 语句调用 6 种方法之一,具体取决于给定框的随机数。这六种方法分析了当前的盒子堆栈,并确定要调用 13 个盒子组合中的哪一个。

package com.chinus;

i
    private static void trace() {
        int boxesUsedSoFar = 0;
        boolean gameIsDone = false;
        int usedOnce;

        while (gameIsDone != true) {
            int number = (int) ((Math.random() * 6) + 1);
            boxesUsedSoFar++;

            System.out.println("Current Box Number : " + number);
            switch (number) {
                case 1:
                    if (number == 1) {
                        box1 = true;

                    }
                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                    } else if (box1 == true && box2 == true) {
                        combo4();
                    } else if (box1 == true && box3 == true) {
                        combo7();
                    } else if (box1 == true) {
                        combo1();
                    }
                    else{
                        restart();
                    }
                    box1=false;
                    break;

                case 2:
                    if (number == 2) {
                        box2 = true;


                    }

                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                    } else if (box1 == true && box2 == true) {
                        combo4();
                    } else if (box2 == true && box3 == true) {
                        combo5();
                    } else if (box2 == true) {
                        combo2();
                    }
                    else{
                        restart();
                    }
                    box2=false;

                    break;
                case 3:
                    if (number == 3) {
                        box3 = true;
                        int count3 = 0;
                    }
                    boolean box3used =false;


                    if (box1 == true && box2 == true && box3 == true) {
                        combo6();
                        box3used =true;
                    } else if (box1 == true && box3 == true) {
                        combo7();
                        box3used =true;
                    } else if (box2 == true && box3 == true) {
                        combo5();
                        box3used =true;
                    }else if (box3 == true) {
                        combo3();
                        box3used = true;
                    }
                    else {
                        restart();
                    }

                    break;
                case 4:
                    if (number == 4) {
                        box4 = true;
                    }
                    if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
                        combo12();
                    } else if (box4 == true && box2 == true & box3 == true && box1 == true) {
                        combo10();
                    } else if (box4 == true && box2 == true & box1 == true) {
                        combo8();
                    } else {
                        restart();
                    }

                    break;

                case 5:
                    if (number == 5) {
                        box5 = true;
                    }
                    if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
                        combo12();
                    } else if (box5 == true && box2 == true & box3 == true && box1 == true) {
                        combo11();
                    } else if (box5 == true && box2 == true & box3 == true) {
                        combo9();
                    } else {
                        restart();
                    }
                    break;

                case 6:
                    if (number == 6) {
                        box6 = true;
                    }
                    if (box4 == true && box5 == true) {
                        combo13();
                        gameIsDone = true;
                    } else {
                        restart();
                    }
                    break;

                default:

            }
            System.out.println("Boxes used so far : " + boxesUsedSoFar);
        }
    }


    private static void restart() {
        box1 = false;
        box2 = false;
        box3 = false;
        box4 = false;
        box5 = false;
        box6 = false;
    }
}
Actual:
Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|
Boxes used so far : 49820
Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|



Expect:Current Box Number : 2
       ___   ___
      |   | |   |
      | 2 | | 3 |
      |___| |___|
Boxes used so far : 49820
Current Box Number : 2
       ___  
      |   | 
      | 2 | 
      |___|

【问题讨论】:

  • 你明白为什么部分代码:switch (number) { case 1: if (number == 1) { box1 = true; } 没有意义?
  • 不,抱歉,我是编码新手
  • if ( number == 1 ) -> 除非 number == 1 已经被测试过,否则这段代码永远不会被执行。

标签: java random boolean


【解决方案1】:

你已经走了大部分路了。

我已经调整了一些东西:

  • 您不需要使用与您的 switch 语句冗余的 if 语句。

当你说

switch(number)
case 1:

你在技术上问

if(number == 1)

所以我们不需要检查 switch 语句中的数字。

  • 在“if”语句中简化了布尔值检查

在if语句中检查布尔变量时,不需要说

if(boolean == true)

你可以简单地说

if(boolean)

默认情况下,这会检查该值是否为真。

就您的代码逻辑而言,我只展示了与您的问题相关的部分。您想在任何语句的开头检查当前框的状态。在“case:1”中,我做的第一件事是检查 box1 是否为真。如果是,我调用了 reset 方法,并将我们踢出了 switch 语句。

如果box1为false(表示我们之前没有使用过),那么我们可以实际运行正常的逻辑,然后将box1设置为true。我评论了你的一些逻辑会去哪里,因为我不确定你想用它做什么。

        int boxesUsedSoFar = 0;
        boolean gameIsDone = false;

        while (gameIsDone != true) {
            int number = (int) ((Math.random() * 6) + 1);
            boxesUsedSoFar++;

            System.out.println("Current Box Number : " + number);
            switch (number) {
                case 1:
                    if (box1) {
                        //box1 is false by default, if it was true here, it means we have a duplicate
                        restart();
                        break;
                    }
                    /*

                    Your code to call a combo method goes here, repeat for each case.

                    */
                    box1 = true;
                    break;
                case 2:
                    if (box2) {
                        restart();
                        break;
                    }
                    box2 = true;
                    break;
                case 3:
                    if (box3) {
                        restart();
                        break;
                    }
                    box3 = true;
                    break;
                case 4:
                    if (box4) {
                        restart();
                        break;
                    }
                    box4 = true;
                    break;
                case 5:
                    if (box5) {
                        restart();
                        break;
                    }
                    box5 = true;
                    break;
                default:
                    if (box6) {
                        restart();
                        break;
                    }
                    box6 = true;
                    break;

            }
            System.out.println("Boxes used so far : " + boxesUsedSoFar);
        }
    }

如果这里有什么不清楚的地方,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多