繁体   English   中英

Do-while循环“字符串索引超出范围”

[英]Do-while loop “string index out of range”

我收到该错误,但我不确定为什么。 我已经尝试了一些不同的东西像开始(!(flag... then == '+'和一个与==到这哪里是线正下方do语句也得到一个错误也是如此。任何人看问题是什么呢?我现在要实现的主要目标是for循环,重复执行以再次将绳子打印在绳子上,并使标志位于左侧或右侧的其他位置。

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) != '+');

}

还有最后一件事,我是否需要在操作权下注释掉的代码?

你有:

String flag = "+";

而且您永远都不会修改它。 因此,当您有条件时:

flag.charAt(ropeLength - 1) != '+'

除非ropeLength等于1,否则它将始终超出范围。

如前所述,关于代码的实际行为,您永远不会修改flag变量。 因此,其第一个字符将始终为'+' ,因此您的循环将始终执行一次。

因此,根据您的目标,我在代码中遇到的第一个问题是flag的使用方式和print/println方法。 如果您想知道'+'在哪里,可以使用StringBuilder这样。

之前:

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("");

后:

StringBuilder flag = new StringBuilder( ropeLength );
for (int i = 0; i < ropeLength / 2; i += 1) {
    flag.append( '-' );
}
flag.append( '+' );

for (int i = 0; i < ropeLength / 2; i += 1) {
    flag.append( '-' );
}

System.out.println( flag.toString() );

// Resetting the StringBuilder for reuse
flag.setLength( 0 );

但是,关于do / while循环,您应该修改整个算法。 编写方式上,如果您如上所述使用StringBuilder,则'+'将无限期地围绕“绳”的中心抖动。 我很确定那不是实际意图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM