当我运行程序时,输出只是连续的,并没有结束。这里的目标是尝试将所有的方式向左或向右移动,然后打印赢得哪方以及转多少回合。我的do-while循环涉及到字符位置
现在的代码被执行,但只从中间,这让我的现在想用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");
}
}
连续输出是什么样的? – 2013-03-16 04:09:05
---- + --------- + -------- + ----等 – tawnpawt 2013-03-16 04:11:58