-2
我正在尝试编写一个code
,根据随机机会将值更改为-1或+1。它基本上是一个穿越街区的人。他从6开始,如果他以1结束,他会赢,但如果他在11结束,他就输了。最后的结果将是这个样子:对于循环错误作为方法
Here we go again... time for a walk!
Walked 37 blocks, and
Landed at Home
Here we go again... time for a walk!
Walked 19 blocks, and
Landed in JAIL
Here we go again... time for a walk!
Walked 13 blocks, and
Landed in JAIL
Here we go again... time for a walk!
Walked 25 blocks, and
Landed in JAIL
我写了下面的代码:
public class Drunk {
public int street;
public double move;
public int i;
public boolean jail;
public static void drunkWalk() {
do {
street = 6;
move = Math.random();
i++;
if (move > 0.5) {
street++;
} else {
street--;
}
} while (street != 1 && street != 11);
if (street == 1) {
jail = false;
} else {
jail = true;
}
for (; ;) { --- } //This is the problem. It treats it as a method.
//How can I fix this?
}
}
将方法内的循环移动。 – Keppil
'for loop'在方法之外 – Rakesh
@Rakesh谢谢:) – Saadat