简体   繁体   English

我无法在数组中存储值,因为它显示错误?

[英]I'm unable to store values in array as it is showing error?

I have to take the size of the array from user input and store it and also I tried to store values in array ac to size but when I run the program, for loop executes once after its showing an error of boundary exception我必须从用户输入中获取数组的大小并将其存储,我还尝试将数组 ac 中的值存储到大小但是当我运行程序时,for 循环在显示边界异常错误后执行一次

 import java.util.Scanner;     

 public class Odd {
    int size;
    int a[] = new int[size];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Size of array");
        size = sc.nextInt();
        for (int i = 0; i < size; i++) {
            System.out.println("enter " + i + "th element");
            a[i] = sc.nextInt();
        }
        System.out.println("your array is");
        for (int i = 0; i < size; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

When you write当你写

public class Odd
{
int size;
int a[]=new int[size];

then whenever Odd is constructed, a is created to be an array with size equal to the current value of size , which is 0.然后每当构造Odd时,都会a创建为一个数组,其大小等于size的当前值,即 0。

When you then write当你再写

size=sc.nextInt();

the size of a does not change to the new value of the size variable. a的大小不会更改为size变量的新值。

There are several things wrong with your code.您的代码有几处错误。

First, the static method main is not able to access size and a because they are instance variables.首先,static 方法 main 无法访问 size 和 a,因为它们是实例变量。 You would need to make them static or make them local variables in the main method.您需要将它们设为 static 或在 main 方法中将它们设为局部变量。

Second, size is zero.第二,规模为零。 You create the array without knowing what the user will enter for size.您在不知道用户将输入什么大小的情况下创建数组。 The array has a size of zero.该数组的大小为零。

Third, you don't need the variable size since you don't really use it anywhere else.第三,您不需要可变大小,因为您实际上并没有在其他任何地方使用它。

import java.util.Scanner;
public class Main{

   public static void main(String[]args){
      Scanner sc=new Scanner(System.in);
   
      System.out.println("Enter Size of array");
      int[] a = new int[sc.nextInt()];
   
      for(int i=0;i<a.length;i++){
         System.out.println("enter "+i+"th element");
         a[i]=sc.nextInt();
      }
   
      System.out.println("your array is");
      for(int i=0;i<a.length;i++){
         System.out.print(a[i]+" ");
      }
      
   }
}

Move the array initialization to inside main method as the size is required while creating array.将数组初始化移动到 main 方法内部,因为创建数组时需要大小。

public class Odd {
    static int size;
    static int a[];
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Size of array");
        size=sc.nextInt();
        a = new int[size];
        for(int i=0;i<size;i++)
        {
            System.out.println("enter "+i+"th element");
            a[i]=sc.nextInt();
        }
        System.out.println("your array is");
        for(int i=0;i<size;i++)
        {
            System.out.print(a[i]+" ");
        }

    }
}

and here is the output

Enter Size of array
3
enter 0th element
1
enter 1th element
2
enter 2th element
3
your array is
1 2 3 

When the array is created, the value of size is defaultly 0. Do this for better results:创建数组时,size 的值默认为 0。这样做以获得更好的结果:

public class Odd
{
    int size;
    int a[];
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Size of array");
        size=sc.nextInt();
        a = new int[size];

Then whatever you have to do you can do...然后无论你必须做什么,你都可以做......

You don't actually need these member variables.您实际上并不需要这些成员变量。

import java.util.Scanner;

public class SizeArray {
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Size of array: ");
        int size = sc.nextInt();
        int a[] = new int[size];

        for(int i = 0; i < size;i ++) {
            System.out.print("enter "+ i +"th element: ");
            a[i]=sc.nextInt();
        }

        System.out.println("your array is");

        for(int i = 0; i < size; i++) {
            System.out.print(a[i]+" ");
        }

        sc.close();
    }
}

You can just put in the main function instead of modifying them after creation.您可以只输入 main function 而不是在创建后修改它们。

Output: Output:

Enter Size of array: 4
enter 0th element: 1
enter 1th element: 2
enter 2th element: 3
enter 3th element: 4
your array is
1 2 3 4

BTW please write your code in more readable format.顺便说一句,请以更具可读性的格式编写您的代码。

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

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