有 Java 编程相关的问题?

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

java为什么我从主类得到index=0和lenght=0的错误?

我知道了

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at RecursiveSquares.main(RecursiveSquares.java:40)

public static void draw(int n, double x, double y, double size) {
        if (n == 0) return;

        drawSquare( x, y, size);

        // 2.2 ratio looks good
        double ratio = 2.2;

        // recursively draw 4 smaller trees of order n-1
        draw(n-1, x - size/2, y - size/2, size/ratio);    // lower left  
        draw(n-1, x - size/2, y + size/2, size/ratio);    // upper left  
        draw(n-1, x + size/2, y - size/2, size/ratio);    // lower right 
        draw(n-1, x + size/2, y + size/2, size/ratio);    // upper right
    }


    // read in an integer command-line argument n and plot an order n recursive
    // squares pattern
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        double x = 0.5, y = 0.5;   // center of square
        double size = 0.5;         // side length of square
        draw(n, x, y, size);

共 (1) 个答案

  1. # 1 楼答案

    在执行程序时,应该传递args数组的值。例如

    java RecursiveSquares 10