Exception- in thread “main” java.lang.ArrayIndexOutOfBoundsException

后端 未结 2 1178
野趣味
野趣味 2021-01-29 06:38
public class Sum {

public static void main(String args[])
{
    int x,y,s; 
    x=Integer.parseInt(args[0]); 
    y=Integer.parseInt(args[1]);
    s=x+y;
   System.out.         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 07:08

    When you run this from the commandline, you need to pass in your values for x and y:

    java Sum 4 5
    

    If you are running this from eclipse, you need to set it up to pass these arguments in every time you hit the green "run" button.

    Eclipse command line arguments

    If you don't pass anything in, then the array "args" will be empty. Then you try and access the first element in the array, but since it is empty, this throws an ArrayIndexOutOfBoundsException.

提交回复
热议问题