- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个使用随机数生成器的堆叠盒子游戏,但每当使用一个数字两次时我就无法重置我的程序?
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(number)
case 1:
你问的是技术性问题
if(number == 1)
所以我们不需要检查 switch 语句中的数字。
在 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/
是否有使用 switch 语句检查数字 0-9 的简单方法?我正在编写一个程序来检查某些字符和数字。就像检查“\0”、“F”或“f”一样,想知道是否还有一种方法可以以类似的方式检查 0-9。我知道我可
我有一些数据需要转换,为此我需要一个超过 50 个案例的切换条件,我需要 3 次相同的案例,但在第三次我需要 50 个案例和一些更多,我不想写两次相同的代码。也许有可能做这样的事情。 switch (
我遇到这种情况,我必须检查两个 GET 变量。在检查语句内的第一个 switch 语句后,必须在第一个 case 循环内的第二个 switch 语句中检查第二个变量。 我无法在这里发布确切的代码,但这
如何使用函数指针代替 switch 语句? 最佳答案 与 ars 发布的链接略有不同的方法:您可以将 switch 语句中的值用作函数指针数组中的数组索引。所以不要写 switch (i) {
我必须评估很多条件。就我而言,我必须做这样的事情: switch(id) { case 5: // switch some other cases here case
switch 按钮位于 switch 语句内,但仅在 switch 语句外部时才有效这是我的代码:
我试图在 switch 语句中有一个 case 跳转到不同的 switch 语句。 在实践中,我希望用户在文本框中键入“关闭页面”,并且在浏览器关闭页面之前,我希望询问用户是否确定。输入“yes”将关
(引用java)我试图确定哪个更好,编写更多代码并可能节省一些计算时间,或者编写更少代码但可能牺牲一些计算时间。这就是我好奇的地方这样做会更有效率吗: switch (availability) {
我正在尝试构建一个 Android 应用程序,该应用程序可以访问加速度计传感器,并在单击按钮时将加速度计值(由 <> 包围)输出到串行 USB。当我更新值并尝试在 onClick 命令中调用它时遇到问
如何使用 Dwoo 模板引擎实现 switch case 语法。 最佳答案 {if 3 == 5} never gonna happen {elseif 3 == 3} if you don'
我想知道一个大的 switch 语句和几个小的 switch 语句之间是否有性能差异。 包含数百个案例的大型 switch 语句。 switch(quest){ case 1:
用户在 2 个选择框中进行选择后,我尝试计算出报值(value)。 看起来 1 需要 2 个 switch 语句。这可能吗? (可能的值比下面的值多得多。为了清楚起见,我删除了它们) var wor
我在主 while 循环内有一个开关,它将运行我的游戏。我正在尝试打破我的开关,以便转到不同的案例。下面的例子更好地解释了这一点: int j = 0; While(1){ switch(j){ ca
我用 Java 创建了一个菜单,其中每个选项都有另一个菜单。我想知道是否可以从内部菜单退出回到主菜单。 编辑:添加了主菜单选项 System.out.println("Main Menu");
我有一个计算新分数的方法。下面的方法有效,但问题是代码本身看起来可以被显着清理。我只是不知道什么是最好的方法。我根据 filterString 和枚举 individualScoreState 分配
在 Lisp 中使用字符串切换语句。 (defun switch(value) (case value (("XY") (print "XY"))
我想做这样的事情: int i = 0; switch(difficulty) { case 1: i++; break; case 2: i--; break; defaul
在 Obj-c 中,我做了一个 switch 语句,我曾经使用 UIsplitviewcontroller 在我的 iPad 应用程序中四处移动现在我想快速地做同样的事情……我试了几个小时,现在我唯一
我想写一个结构如下的报告: \begin{document} \input[option=a]{class} \input[option=b]{class} \in
我正在认真阅读 ngSwitch 的 AngularJS API 引用。当我谈到那部分时的指令: place an expression on the on="..." attribute (or t
我是一名优秀的程序员,十分优秀!