当我运行程序时,输出只是连续的并且不会结束。这里的目标是尝试让 + 一直到左边或右边,然后打印哪一方获胜以及它花了多少圈。
代码现在正在执行,但它只从中间向左和向右移动一个空格,这让我认为它现在使用 for 循环
package test;
import java.util.Scanner;
import java.lang.Math;
public class Test {
public static int MAX_LENGTH = 21;
public static int MIN_LENGTH = 5;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length of the rope: ");
int ropeLength = keyboard.nextInt();
while (ropeLength < MIN_LENGTH || ropeLength > MAX_LENGTH || ropeLength % 2 != 1) {
System.out.println("Thats not a valid length (odd number between 5 and 21)");
System.out.print("Enter the length of the rope: ");
ropeLength = keyboard.nextInt();
}
char a;
String flag = " ";
for (int i = 0; i < ropeLength / 2; i += 1) {
flag += "-";
}
flag += "+";
for (int i = 0; i < ropeLength / 2; i += 1) {
flag += "-";
}
System.out.println("");
do {
flag = "";
double rand = Math.random();
int i;
if (rand > 0.5) {
for (i = 0; i < (ropeLength / 2) - 1; i++) {
flag += "-";
}
flag += "+";
for (i = 0; i < (ropeLength / 2) + 1; i++) {
flag += "-";
}
System.out.println( flag );
}
if (rand < 0.5) {
for (i = 0; i < (ropeLength / 2) + 1; i++) {
flag += "-";
}
flag += "+";
for (i = 0; i < (ropeLength / 2) - 1; i++) {
flag += "-";
}
System.out.println( flag );
}
} while (flag.charAt(1) != '+' || flag.charAt(ropeLength) != '+');
if (flag.charAt(0) == '+') {
System.out.println("\nLeft side wins!");
}
else {
System.out.println("\nRight side wins!");
}
System.out.println("It took steps");
}
}