有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Dowhile循环“字符串索引超出范围”

我得到了那个错误,我不知道为什么。我尝试了一些不同的方法,比如从(!(flag... then == '+'开始,还有一种是从==到a,do语句正下方的行也会出现错误。有人看到问题了吗?我现在想要达到的主要目标是让循环重复,在不同的位置向左或向右打印绳子

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

}

还有最后一件事,我是否需要在do下面注释掉的代码


共 (1) 个答案

  1. # 1 楼答案

    你有:

    String flag = "+";
    

    你永远不会修改它。因此,当你有条件:

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

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

    关于代码的实际行为,正如我提到的,您从不修改flag变量。所以它的第一个字符总是'+',因此循环总是执行一次

    因此,根据您的目标,我在代码中看到的第一个问题是使用flagprint/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循环,您应该修改整个a算法。这样写的话,如果像我上面提到的那样使用StringBuilder,'+'只会无限期地围绕“绳索”的中心抖动。我很确定这不是真正的意图