I'm getting that error and I'm not sure why. I've tried a few different things like starting with (!(flag... then == '+' and one with == to a which is where the line right below the do statement also gets an error as well. Anyone see the problem? The main goal I'm trying to get right now is the for loops to repeat to print the rope again with the flag in a different place left or right.
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) != '+');
}
}
and as for the do while loop, my for loops seem to only be repeating once or twice.
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) != '+');
}
and one final thing, do i need that code that i have commented out right under the do?
flag.charAt(ropeLength - 1)be?