| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
|* |
我需要打印类似于上面ASCII图的内容,用于解决赌徒破产问题。其中赌注和目标作为参数传入。最左边的|代表0美元,最右边的|代表目标金额,*代表手头现金。我的程序如下:
public class RuinPath {
public static void main(String[] args) {
// TODO - Your solution
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
{
int i = 0;
if (i == 0) {
System.out.print("|");
i++;
while (i < stake) {
System.out.print(" ");
i++;
if (i == stake) {
System.out.print("*");
i++;
while (i > stake && i < goal) {
System.out.print(" ");
i++;
if (i == goal) {
System.out.print("|");
i = 0;
System.out.println();
{
if (Math.random() < 0.5) {
stake++; // win $1
} else {
stake--; // lose $1
}
if (stake == 1 || stake == goal - 1);
break;
}
}
}
}
}
}
}
}
}
这个程序输出的是:
| * |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
*
为什么我的程序无法循环,以便我可以通过左侧的|表示0美元一直到结束?我将i设置为0;在循环结束时,所以当它再次回到循环时,它应该重新循环,直到赌注达到1或少于目标。但它会从程序的中间重新开始循环,而不是从开头重新循环。