簡體   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