오류가 발생하고 이유가 확실하지 않습니다. 나는 (!(flag... then == '+'
으로 시작하고 ==
으로 시작하는 것과 같은 몇 가지 다른 것들을 시도해 보았다. do 문 바로 아래의 줄 또한 오류를 얻는다. 누구든지 문제를 봤어? 내가 지금 당장 얻으려고하는 주된 목표는 왼쪽 또는 오른쪽으로 다른 위치에 플래그가있는 로프를 다시 인쇄하는 for 루프를 반복하는 것입니다.Do-while 루프 "문자열 인덱스가 범위를 벗어났습니다."
및 루프 동안, 내 루프의 할 일에 대한 같은
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 {
//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) != '+');
}
한 최종 것은, 내가 바로 할 하에서 댓글을 달았 그 코드가 필요합니까?
String flag = "+";
을하고 당신이 그것을 수정하지 :
ropeLength = 13이라고 가정하십시오.'flag.charAt (ropeLength - 1)'의 값은 무엇이되어야합니까? – Ingo
변수 플래그의 길이는 1입니다. –