我收到了那个错误,我不知道为什么。我尝试了一些不同的方法,例如从 to a 开始,(!(flag... then == '+'
其中==
do 语句正下方的行也出现错误。有人看到问题了吗?我现在想要实现的主要目标是重复循环以再次打印绳索,并将标志放在左侧或右侧的不同位置。
package program2;
import java.util.Scanner;
import java.lang.Math;
public class Program2 {
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) {
System.out.print("-");
}
System.out.print(flag);
for (int i = 0; i < ropeLength / 2; i += 1) {
System.out.print("-");
}
System.out.println("");
do {
//a = flag.charAt(ropeLength);
double rand = Math.random();
if (rand > 0.5) {
for (int i = 0; i < (ropeLength / 2) - 1; i++) {
System.out.print("-");
}
System.out.print(flag);
for (int i = 0; i < (ropeLength / 2) + 1; i++) {
System.out.print("-");
}
if (rand < 0.5) {
for (int i = 0; i < (ropeLength / 2) + 1; i++) {
System.out.print("-");
}
System.out.print(flag);
for (int i = 0; i < (ropeLength / 2) - 1; i++) {
System.out.print("-");
}
}
}
} while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+');
}
}
至于 do while 循环,我的 for 循环似乎只重复一次或两次。
do {
//a = flag.charAt(ropeLength);
double rand = Math.random();
if (rand > 0.5) {
for (int i = 0; i < (ropeLength / 2) - 1; i++) {
System.out.print("-");
}
System.out.print(flag);
for (int i = 0; i < (ropeLength / 2) + 1; i++) {
System.out.print("-");
}
if (rand < 0.5) {
for (int i = 0; i < (ropeLength / 2) + 1; i++) {
System.out.print("-");
}
System.out.print(flag);
for (int i = 0; i < (ropeLength / 2) - 1; i++) {
System.out.print("-");
}
}
}
} while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+');
}
最后一件事,我是否需要我在 do 下注释掉的代码?