0

そのエラーが発生しましたが、その理由がわかりません。do ステートメントのすぐ下の行でもエラーが発生する a で開始する(!(flag... then == '+'など、いくつかの異なることを試しました。==誰もが問題を見ますか?私が今取得しようとしている主な目標は、 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 ループは 1 回か 2 回しか繰り返されていないようです。

 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 のすぐ下でコメントアウトしたコードが必要ですか?

4

1 に答える 1

1

あなたが持っている:

String flag = "+";

変更することはありません。したがって、条件がある場合:

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

ただし、ropeLength が 1 の場合は、常に範囲外になります。

コードの実際の動作については、前述したように、flag変数を変更することはありません。そのため、最初の文字は常に になる'+'ため、ループは常に 1 回実行されます。

したがって、あなたの目標に従ってあなたのコードで最初に目にする問題は、使用方法flagprint/println方法です。がどこにあるか知りたい場合は、この方法'+'で a を使用できます。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 を使用する'+'と、「ロープ」の中心を無期限に揺らすだけになります。それが本当の意図ではないことは確かです。

于 2013-03-16T01:00:04.163 に答える