Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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 在将数据点添加到数组之前,请检查用户输入的数据点数量,以避免索引越界异常_Java_Arrays_Java.util.scanner - Fatal编程技术网

Java 在将数据点添加到数组之前,请检查用户输入的数据点数量,以避免索引越界异常

Java 在将数据点添加到数组之前,请检查用户输入的数据点数量,以避免索引越界异常,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,在将数据点添加到数组之前,请检查用户输入的数据点数量,以避免索引越界异常 在添加到阵列之前如何检查 while (userInput>MAX) { System.out.println("Amount of numbers too large. Input number less than 50."); System.out.println("Enter the amount of numbers that are in

在将数据点添加到数组之前,请检查用户输入的数据点数量,以避免索引越界异常 在添加到阵列之前如何检查

    while (userInput>MAX) {
        System.out.println("Amount of numbers too large. Input number less than 50.");
        System.out.println("Enter the amount of numbers that are in this data set: ");
        userInput = scanner.nextInt();
    }
    int numIntegers = userInput;

    //have the user input the numbers delineated by a space
    System.out.println("Enter each number separated by a space:");
    int[] integerArray = new int[numIntegers];
    
    for(int index=0;index<MAX;index++){
        integerArray[index] =  scanner.nextInt();
    }
while(用户输入>最大值){
System.out.println(“数字量太大,输入数字小于50”);
System.out.println(“输入此数据集中的数字量:”);
userInput=scanner.nextInt();
}
int numIntegers=userInput;
//让用户输入空格表示的数字
System.out.println(“输入每个数字,用空格分隔:”);
int[]integerArray=新的int[numitegers];

对于(int index=0;index),在创建数组之前,最好先询问用户要输入多少数据点,然后再创建数组

简单示例

System.out.println("How many data points : ");
int ele = input.nextInt();
}

 double [] dataPoints = new double[ele];
如果要填充数组以避免索引越界异常

 for(int i=0;i<ele;i++){
               dataPoints[i] = input.nextDouble();
            }
for(int i=0;iMAX){
System.out.println(“数字量太大,输入数字小于50”);
System.out.println(“输入此数据集中的数字量:”);
userInput=scanner.nextInt();
}
int numIntegers=userInput;
//让用户输入空格表示的数字
System.out.println(“输入每个数字,用空格分隔:”);
int[]integerArray=新的int[numitegers];

for(int index=0;indexcoudn)不知道如何添加所有代码。只是想出来并添加了我使用numIntegers更改max的代码。for(int index=0;indexi)可以只执行while循环并不断询问,直到array.length大小等于用户输入的数字,但我宁愿让它在一行中完成。您可以使用for循环来完成这一操作(int i=0;我只是编辑了你的代码。似乎你是在要求编写一段代码。这通常不会吸引很多读者和积极的反馈。想想看,你是否可以将这一问题重新表述为一个人们觉得有趣且易于搜索的问题。如果你不想提前问多少,那么你必须使用可扩展的数据结构结构类似于
ArrayList
  while (userInput>MAX) {
        System.out.println("Amount of numbers too large. Input number less than 50.");
        System.out.println("Enter the amount of numbers that are in this data set: ");
        userInput = scanner.nextInt();
    }
    int numIntegers = userInput;

    //have the user input the numbers delineated by a space
    System.out.println("Enter each number separated by a space:");
    int[] integerArray = new int[numIntegers];
    
    for(int index=0;index<numIntegers;index++){
        integerArray[index] =  scanner.nextInt();
    }