netbeans java 代码退出,退出代码为 1,没有明显原因

netbeans java code exits with exit code 1 with no apparant reason

第一个class是这个

/*
 * To change this license header, choose License Headers in Project  Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package logical.conundrum;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Tuvanen Kiruitsu
*/
public class LogicalConundrum {
   /**
    * @param args the command line arguments
    */
    public static int [] RNG_OUT;
    public static String[][] inv;
    public static float hp;
    public static int[] Room;
    public static int RNG_SET; 
    public static void main(String[] args) {
        inv = new String[5][5];
        System.out.print("How many numbers do you want > ");
        Scanner input = new Scanner(System.in);
        int sc_in = input.nextInt();
        int loop_1 = sc_in;
        int sc_out = sc_in++;
        RNG_SET = sc_in++;
        RNG_OUT = new int[sc_out];
        RNG_OUT[0] = 0;
        RNG.RNG(); /fails here

        for (int a = loop_1; a >= 1; a--){
            System.out.println (RNG_OUT[a]); 
        }
    }
}

和第一个引用的第二个

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logical.conundrum;

/**
*
* @author Tuvanen Kiruitsu
*/
public class RNG {

    /**
     *
     */
        static int rng[];
        static void RNG(){
            int RNG_SETUP = LogicalConundrum.RNG_SET;
            rng = new int[4];
            for (int c = RNG_SETUP; c >= 1; c--){
                simulateRNG();
                LogicalConundrum.RNG_OUT[c] = rng[2]; //fails here
        }
    }
    static int simulateRNG(){
        byte y = 1;
        tickRNG(y);
        y--;
        tickRNG(y);
        return rng[2];
    }
    static void tickRNG(byte y) {
        rng[0] = (byte) (5 * rng[0] + 1);
        boolean c = (rng[1] & 0x80) != 0;
        boolean z = (rng[1] & 0x10) != 0;
        rng[1] = (byte) (2 * rng[1]);
        if (c == z){
            rng[1]++;
        }
        rng[2+y] = (byte) (rng[1] ^ rng[1]);
    }
}

而且不管我放什么总是returns这个

run:

How many numbers do you want > 10

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

at logical.conundrum.RNG.RNG(RNG.java:23)

at logical.conundrum.LogicalConundrum.main(LogicalConundrum.java:32)

C:\Users\user\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 4 seconds)

代码总是这样退出 我不知道哪里出了问题,也不知道如何解决,感谢所有帮助

根据您的输入 10,您的数组大小为 10。在 RNG.RNG() 循环中,您想从索引 11 开始,是什么原因导致越界异常。更改此行:

int RNG_SETUP = LogicalConundrum.RNG_SET;

int RNG_SETUP = LogicalConundrum.RNG_OUT.length - 1;

最后一个循环内部也有问题再次索引越界。改变这个:

for (int a = loop_1; a >= 1; a--){

至:

for (int a = RNG_OUT.length - 1; a >= 1; a--){

如果你在数组上循环那么你应该总是使用数组的长度作为限制而不是一些变量。如果您打算在代码中遍历整个数组,那么您应该将循环内的限制更改为 x >= 0.