Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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 我的程序失败,出现ArrayIndexOutofBoundsSeption错误_Java_Swing_Indexoutofboundsexception - Fatal编程技术网

Java 我的程序失败,出现ArrayIndexOutofBoundsSeption错误

Java 我的程序失败,出现ArrayIndexOutofBoundsSeption错误,java,swing,indexoutofboundsexception,Java,Swing,Indexoutofboundsexception,您好,我的程序无法使用ArrayIndexOutofBoundsSeption运行。我在运行时遇到此错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -35 at RoulleteChecker.main(RoulleteChecker.java:134) 这是我的密码: import javax.swing.JOptionPane; import javax.swing.JTextArea; p

您好,我的程序无法使用
ArrayIndexOutofBoundsSeption
运行。我在运行时遇到此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -35
at RoulleteChecker.main(RoulleteChecker.java:134)
这是我的密码:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class RoulleteChecker {

      public static void main( String args[] )
      {

          int a[] = new int[37];

          for ( long roll = 1; roll <=99999999; roll++ ) {      
             a[0] = 1 + ( int ) ( Math.random() * 34); 

             switch ( a[0] ) {

               case 0:
                  ++a[1];
                   break; 

                case 1:
                   ++a[2];
                  break;

                case 2:
                   ++a[3];
                   break;

                case 3:
                   ++a[4];
                   break;

                case 4:
                   ++a[5];
                   break;

                case 5:
                   ++a[6];
                   break;
                case 6:
                    ++a[7];
                    break;
                case 7:
                    ++a[8];
                    break;
                case 8:
                    ++a[9];
                    break;
                case 9:
                    ++a[10];
                    break;
                case 10:
                    ++a[11];
                    break;
                case 11:
                    ++a[12];
                    break;
                case 12:
                    ++a[13];
                    break;
                case 13:
                    ++a[14];
                    break;
                case 14:
                    ++a[15];
                    break;
                case 15:
                    ++a[16];
                    break;
                case 16:
                    ++a[17];
                    break;
                case 17:
                    ++a[18];
                    break;
                case 18:
                    ++a[19];
                    break;
                case 19:
                    ++a[20];
                    break;
                case 20:
                    ++a[21];
                    break;
                case 21:
                    ++a[22];
                    break;
                case 22:
                    ++a[23];
                    break;
                case 23:
                    ++a[24];
                    break;
                case 24:
                    ++a[25];
                    break;
                case 25:
                    ++a[26];
                    break;
                case 26:
                    ++a[27];
                    break;
                case 27:
                    ++a[28];
                    break;
                case 28:
                    ++a[29];
                    break;
                case 29:
                    ++a[30];
                    break;
                case 30:
                    ++a[31];
                    break;
                case 31:
                    ++a[32];
                    break;
                case 32:
                    ++a[33];
                    break;
                case 33:
                    ++a[34];
                    break;
                case 34:
                    ++a[35];
                    break;
             } 

         } 

         JTextArea outputArea = new JTextArea();

         outputArea.setText( "Lets see: " + a[0-35] );

         JOptionPane.showMessageDialog( null, outputArea,
             "Searching for the frequency: 99999999 times", JOptionPane.INFORMATION_MESSAGE );  
         System.exit( 1 );

     }  
} 
import javax.swing.JOptionPane;
导入javax.swing.JTextArea;
公共级鲁勒特切克{
公共静态void main(字符串参数[])
{
int a[]=新int[37];
对于(长滚动=1;滚动这是Java:

 a[0-35]
实际上是指:

 a[-35]
是什么让你认为-35是一个有效的索引?你看,你需要修复错误的所有信息都在那里-你只需要仔细阅读异常消息。它告诉你错误的位置,它告诉你无效的索引

和BTW:考虑你的转换语句。

你真的认为你需要写下一个或多或少能做到的开关吗

case i:
  i++; 

错误来自此行:

outputArea.setText( "Lets see: " + a[0-35] );
-
符号是算术减号运算符。因此,0-35是
-35
,这当然不是一个有效的索引。我猜您想打印0到35的范围,可以通过以下方法完成:


首先,所有这些巨大的开关都可以用这个来代替:

++a[a[0] + 1];
然后,正如其他人已经指出的,
a[0-35]
并不意味着你想要什么,它不会神奇地抓住数组的位置0到35。它只是0减去35的数学结果,即-35。数组上没有位置-35

要将数组表示为字符串,我们可以使用Java 8流:

IntStream.of(a).limit(36).boxed().collect(Collectors.toList()).toString().replace(", ", "\n")
它的作用是:

  • 这将把
    int[]
    转换为
    IntStream
    (a)
  • 部分的
    IntStream.of)
    
  • 让limit只获取前36个元素(因为您希望元素从0到35)。这就是
    .limit(36)
    的作用
  • 然后,
    IntStream
    将转换为
    .boxed()
    )部分
  • 然后它将被转换为
    列表
    (collector.toList())
  • )部分
  • 然后它将被转换为
    字符串
    .toString()
    部分)
  • 由于
    字符串
    太长,无法在一行中显示,因此最好添加一些换行符,这就是
    .replace(“,”,“\n”)
    所做的
  • 最后,您不需要
    系统。退出(1);
    ,它在这里没有任何用途

    这样,这就是您的结果代码:

    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class RoulleteChecker {
    
        public static void main(String args[]) {
    
            int a[] = new int[37];
    
            for (long roll = 1; roll <= 99999999; roll++) {
                a[0] = 1 + (int) (Math.random() * 34);
                ++a[a[0] + 1];
            }
    
            JTextArea outputArea = new JTextArea();
    
            outputArea.setText("Lets see: " + IntStream.of(a).limit(36).boxed().collect(Collectors.toList()).toString().replace(", ", "\n"));
    
            JOptionPane.showMessageDialog(null, outputArea,
                    "Searching for the frequency: 99999999 times", JOptionPane.INFORMATION_MESSAGE);
    
        }
    }
    
    这里还有一些不同之处:

    • 因为数组的第0个位置只是暂时保存新生成的数字,所以让它完全从数组中取出。这就是
      r
      变量。因为我删除了数组的第0个位置,所以我删除了
      ++a[a[0]+1]
      上的
      +1
      ,它变成了
      ++a[r]

    • 我将次数(9999999999)移动到一个常量。这使得它更容易在需要时进行更改

    • 同样,由于我删除了数组的第0个位置,因此我也从计算随机数的行中删除了
      1+
      。此外,我让它动态获取数组的大小,因此在创建数组和随机访问其中一个位置时,您无需跟踪数组的大小

    • asString
      方法应该非常简单明了。唯一的问题是
      i+1
      ,其中
      +1
      的目的是显示从1开始的索引,而不是从0开始

    • asString
      方法中的
      StringBuilder
      构造函数上的参数只是一个总的
      String
      大小,对于性能而言,它并不重要


    你认为这个
    a[0-35]
    有什么作用?巨大的开关块可以变成1-3行代码。通过迭代来打印数组的内容。删除该
    a[0-35]
    。对不起,输入错误;我的意思是-35(因为0-35)。不客气-请随意投票选出正确答案并接受其中一个!我没有足够的声誉来投票:/。无论如何,再次感谢:)你能告诉我一些其他事情吗?我希望每一行都以一个数字开头。但我希望这个数字在其他行中会有所不同,如1:\n 2:\n 3等,从0到34。你能帮我吗?谢谢
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class RoulleteChecker {
    
        public static void main(String args[]) {
    
            int a[] = new int[37];
    
            for (long roll = 1; roll <= 99999999; roll++) {
                a[0] = 1 + (int) (Math.random() * 34);
                ++a[a[0] + 1];
            }
    
            JTextArea outputArea = new JTextArea();
    
            outputArea.setText("Lets see: " + IntStream.of(a).limit(36).boxed().collect(Collectors.toList()).toString().replace(", ", "\n"));
    
            JOptionPane.showMessageDialog(null, outputArea,
                    "Searching for the frequency: 99999999 times", JOptionPane.INFORMATION_MESSAGE);
    
        }
    }
    
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class RoulleteChecker {
    
        private static final int TIMES = 99999999;
    
        public static void main(String args[]) {
    
            int a[] = new int[36];
    
            for (long roll = 1; roll <= TIMES; roll++) {
                int r = (int) (Math.random() * a.length);
                ++a[r];
            }
    
            JTextArea outputArea = new JTextArea();
    
            outputArea.setText("Lets see:\n" + asString(a));
    
            JOptionPane.showMessageDialog(null, outputArea,
                    "Searching for the frequency: " + TIMES + " times", JOptionPane.INFORMATION_MESSAGE);
        }
    
        private static String asString(int[] s) {
            StringBuilder sb = new StringBuilder(8 * s.length);
            for (int i = 0; i < s.length; i++) {
                sb.append(i + 1).append(": ").append(s[i]).append("\n");
            }
            return sb.toString();
        }
    }