简体   繁体   English

网格图问题/错误

[英]Grid drawing issue/error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Grid.main(Grid.java:25) 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:在Grid.main处为5(Grid.java:25)

The above error occurs when attempting to compile: 尝试编译时发生上述错误:

class Grid {
// Grid drawing method
    static void drawGrid(int n) {
        StdDraw.setXscale(0, 2*n);
        StdDraw.setYscale(0, 2*n);
// StdDraw.jar setter for scale method called
        for (int v = 1; v <= n; v++) {
            // variable colours lower left square red.
            for (int k = 1; k <= n; k++) {
                if ((v+k) % 2 == 0)
                    StdDraw.setPenColor(StdDraw.RED);
                else
                    StdDraw.setPenColor(StdDraw.BLACK);
                StdDraw.filledSquare(2*v-1, 2*k-1, 1);
            }
        }
    }

    public static void main(String[] args) {
        drawGrid(Integer.parseInt(args[5])); 
        // calls the drawGrid method to draw the grid with defined values
    }
}

I know this can be interpreted as a simple program and a basic error to most of you, just an innocent little red and black grid but it's stumped me. 我知道这可以解释为一个简单的程序,对大多数人来说是一个基本错误,只是一个无辜的红色和黑色小网格,但这让我很困惑。 I think I understand what the error means, I'm attempting to access a size of grid the program can't draw? 我想我知道错误的含义,我正在尝试访问程序无法绘制的网格大小? This is what I've gathered from research from resources on this site and others, I just do not understand why a value of 5 is throwing the error. 这是我从该站点以及其他站点上的资源研究中收集的信息,我只是不明白为什么5值会引发错误。 I tried changing that value and it's indiscriminate no matter the number. 我尝试更改该值,无论数量多少,它都是不加区别的。 I think there may be an issue with the increment calculations or something? 我认为增量计算可能有问题吗?

This program successfully compiled on Netbeans, according to my colleagues but on my home Eclipse set up I'm getting the "OutOfBounds" error. 据我的同事说,该程序已在Netbeans上成功编译,但在我的家庭Eclipse设置上,出现“ OutOfBounds”错误。

Of course, I realise this may be slightly trivial but have done everything I can to understand why my colleagues code is failing, and I can shed little light on it myself either. 当然,我意识到这可能有点琐碎,但是已经尽了一切努力来理解为什么我的同事代码失败了,我自己也无法对此有所了解。 So, I pose it you, Stack Overflow community. 所以,我向您介绍一下Stack Overflow社区。 What could be the reasoning for this error? 该错误的原因可能是什么?

PS I'm curious if this actually works in Netbeans as I'm taking moderately trustworthy colleagues words for it but, don't have the means to setting up the JDK on this system at the moment. PS我很好奇这是否真的可以在Netbeans中使用,因为我正在接受一些值得信赖的同事的话,但是目前还没有办法在该系统上设置JDK。 I would be most appreciative if anyone who does have access to the software could test this? 如果可以访问该软件的任何人都可以测试此软件,我将不胜感激。

The error appears to be occurring in your main method. 该错误似乎是在您的主要方法中发生的。 args[5] is the 6th command line argument to your program. args[5]是程序的第6个命令行参数。 Based on your code, I'm assuming that it should be drawGrid(Integer.parseInt(args[0])); 根据您的代码,我假设它应该是drawGrid(Integer.parseInt(args[0])); . This will access the first command-line argument. 这将访问第一个命令行参数。

As far as whether it works in Netbeans, the only way to find out is to try it! 至于它是否可以在Netbeans中运行,找出它的唯一方法就是尝试一下!

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

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