Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,I';m嵌套循环,但它';It’他给了我一个错误信息_Java_Loops_For Loop - Fatal编程技术网

在Java中,I';m嵌套循环,但它';It’他给了我一个错误信息

在Java中,I';m嵌套循环,但它';It’他给了我一个错误信息,java,loops,for-loop,Java,Loops,For Loop,我尝试了一个完全相同格式的for循环练习,它不会产生像这样的错误消息。在我引入外环(int j)之前,内环(int i)工作得很好: private static void mBTI(){ 扫描仪控制台=新扫描仪(System.in); Random rand=新的Random(); //练习圈 int wtf=console.nextInt(); 对于(int k=1;k您可能遇到索引越界异常。请确保使用randType=randType%types.length(或其他使randType保持

我尝试了一个完全相同格式的for循环练习,它不会产生像这样的错误消息。在我引入外环(int j)之前,内环(int i)工作得很好:

private static void mBTI(){
扫描仪控制台=新扫描仪(System.in);
Random rand=新的Random();
//练习圈
int wtf=console.nextInt();

对于(int k=1;k您可能遇到索引越界异常。请确保使用randType=randType%types.length(或其他使randType保持在边界内的东西)。

那么…错误消息是什么?它引用了哪一行?我们目前无法读取您的想法。我认为它在
System.out.print(types)上失败[randType];
带有
数组索引越界异常
它给我的基本错误消息是:java.lang.ArrayIndexOutOfBoundsException:9看起来像是越界异常。如果你为
数量
输入一个大数字,那么
randType
将变大!我看到了;我没有敲“循环”回到0。谢谢你,可怕的袋熊。
private static void mBTI(){
    Scanner console = new Scanner(System.in);
    Random rand = new Random();

    //Practice loop
    int wtf = console.nextInt();
    for (int k = 1; k <= wtf; k++){
        for (int m = 1; m <= 2; m++){
            System.out.println("derp");
        }
    }        

    //Error loop
    System.out.println("How many results do you want? (Type a numerical value.)");
    int amount = console.nextInt();
    String[] types = {"E","I","N","S","F","T","J","P"};
    int loop = 0;
    for(int j = 1; j <= amount; j++){
        for(int i = 1; i <= 4; i++){
            int randType = rand.nextInt(2);
            randType = loop+randType;
            System.out.print(types[randType]);
            loop = loop+2;
        }
        System.out.println("");
    }
}