在Java中超过数组限制后显示消息

在Java中超过数组限制后显示消息,java,arrays,Java,Arrays,我试图在数组中输入一系列数字后显示一条消息 我试图在超出数组限制后打印数组。数组中的输入工作正常,但在超出数组限制后显示消息不起作用 import java.util.Scanner; public class Array { public static void main (String[] args) { int i; Scanner input= new Scanner(System.in); int[] values = n

我试图在数组中输入一系列数字后显示一条消息 我试图在超出数组限制后打印数组。数组中的输入工作正常,但在超出数组限制后显示消息不起作用

import java.util.Scanner;
public class Array
{
    public static void main (String[] args)
    {
        int i;
        Scanner input= new Scanner(System.in);
        int[] values = new int[10];
        {
            System.out.print("please insert a value : "  );
            for( i = 0 ;i < values.length; i++)
            {                   
                try 
                {
                    values[i] = input.nextInt();    
                } 
                catch (Exception e) 
                {
                    System.out.println("The value you have input is not a valid integer");
                }

            }
            if (i> values.length)
            {
                 System.out.println("Array limit exceeded");
                 for(i =0 ;i<values.length ; i++)
                 {
                     System.out.println(values[i]);
                 }
            }
        }
    }
}
在此之后:

        for( i = 0 ;i < values.length; i++)
        {
            try 
            {
                values[i] = input.nextInt();    
            } 
            catch (Exception e) 
            {
                System.out.println("The value you have input is not a valid integer");
            }

        }
因此,永远不会超过数组限制

建议:

我想你想要这样的东西:

public class Array {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            int[] values = new int[10];
            int i = 0;
            while (true) {
                System.out.print("Insert a value : ");
                try {
                    values[i] = input.nextInt();
                    i++;
                } catch (InputMismatchException ime) {
                    System.out.println("The value is not a valid integer");
                    input.next();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("Array limit exceeded");
                    break;
                }
            }
        }
    }
}
如果尝试添加第11个元素,将获得ArrayIndexOutOfBoundsException。

在此之后:

        for( i = 0 ;i < values.length; i++)
        {
            try 
            {
                values[i] = input.nextInt();    
            } 
            catch (Exception e) 
            {
                System.out.println("The value you have input is not a valid integer");
            }

        }
因此,永远不会超过数组限制

建议:

我想你想要这样的东西:

public class Array {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            int[] values = new int[10];
            int i = 0;
            while (true) {
                System.out.print("Insert a value : ");
                try {
                    values[i] = input.nextInt();
                    i++;
                } catch (InputMismatchException ime) {
                    System.out.println("The value is not a valid integer");
                    input.next();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("Array limit exceeded");
                    break;
                }
            }
        }
    }
}
如果尝试添加第11个元素,将获得ArrayIndexOutOfBoundsException。

在此之后:

        for( i = 0 ;i < values.length; i++)
        {
            try 
            {
                values[i] = input.nextInt();    
            } 
            catch (Exception e) 
            {
                System.out.println("The value you have input is not a valid integer");
            }

        }
因此,永远不会超过数组限制

建议:

我想你想要这样的东西:

public class Array {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            int[] values = new int[10];
            int i = 0;
            while (true) {
                System.out.print("Insert a value : ");
                try {
                    values[i] = input.nextInt();
                    i++;
                } catch (InputMismatchException ime) {
                    System.out.println("The value is not a valid integer");
                    input.next();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("Array limit exceeded");
                    break;
                }
            }
        }
    }
}
如果尝试添加第11个元素,将获得ArrayIndexOutOfBoundsException。

在此之后:

        for( i = 0 ;i < values.length; i++)
        {
            try 
            {
                values[i] = input.nextInt();    
            } 
            catch (Exception e) 
            {
                System.out.println("The value you have input is not a valid integer");
            }

        }
因此,永远不会超过数组限制

建议:

我想你想要这样的东西:

public class Array {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            int[] values = new int[10];
            int i = 0;
            while (true) {
                System.out.print("Insert a value : ");
                try {
                    values[i] = input.nextInt();
                    i++;
                } catch (InputMismatchException ime) {
                    System.out.println("The value is not a valid integer");
                    input.next();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("Array limit exceeded");
                    break;
                }
            }
        }
    }
}
如果您尝试添加第11个元素,将获得ArrayIndexOutOfBoundsException。

您没有达到If条件。退出循环时,i=values.length。您没有达到if条件。退出循环时,i=values.length。您没有达到if条件。退出循环时,i=values.length。您没有达到if条件。退出循环时,i=values.length。