gpt4 book ai didi

java - 如何重置 switch 语句中的值?

转载 作者:行者123 更新时间:2023-12-02 05:57:01 24 4
gpt4 key购买 nike

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

1) 选择一种实现方式,可以在创建框时对其进行跟踪,也可以直接找到解决方案。2) while 循环控制接下来的三个步骤。3)选择一个盒子4)判断这个盒子是否可以放置
5)放置一个盒子
我使用了 6 个 boolean 变量,每个变量代表一个盒子。 switch 语句根据给定框的随机数调用 6 种方法之一。这 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 语句冗余的 if 语句。

当你说

switch(number)
case 1:

你问的是技术性问题

if(number == 1)

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

  • 简化了“if”语句中 boolean 值的检查

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

if(boolean == true)

你可以简单地说

if(boolean)

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

就您的代码逻辑而言,我仅显示了与您的问题相关的部分。您想要在任何语句的开头检查当前框的状态。在“case:1”中,我做的第一件事是检查 box1 是否为真。如果是,我调用重置方法,并将我们踢出 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);
}
}

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

关于java - 如何重置 switch 语句中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56003298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com