为什么我的代码加上try catch之后反而编译不通过

J2EE 码拜 9年前 (2015-04-18) 950次浏览 0个评论
public static void main(String[] args) {
		try {
			Scanner in = new Scanner(System.in);
			String ss = in.nextLine();

			String[] temp = ss.split(" ");
			int person = Integer.parseInt(temp[0]);
			int game = Integer.parseInt(temp[1]);
			System.out.println("person :"+ person +"game "+ game);
	  }catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("Error of Input!");
		}
		System.out.println(game,person);
                }

运行的时候显示找不到person和game,小白求解

为什么我的代码加上try catch之后反而编译不通过
20分
public static void main(String[] args) {
      int person=0;
      int game =0;
try {
Scanner in = new Scanner(System.in);
String ss = in.nextLine();

String[] temp = ss.split(” “);
 person = Integer.parseInt(temp[0]);
 game = Integer.parseInt(temp[1]);
System.out.println(“person :”+ person +”game “+ game);
  }catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“Error of Input!”);
}
System.out.println(game,person);
                }

这样就行了 ,作用域的问题。

为什么我的代码加上try catch之后反而编译不通过
10分
 System.out.println(game,person);  看看api  没有 2个 参数的。

其次 ,你 game,person 也访问不到 ,变量的作用范围 不一样 。

为什么我的代码加上try catch之后反而编译不通过
引用 1 楼 u012463264 的回复:

public static void main(String[] args) {
      int person=0;
      int game =0;
try {
Scanner in = new Scanner(System.in);
String ss = in.nextLine();

String[] temp = ss.split(” “);
 person = Integer.parseInt(temp[0]);
 game = Integer.parseInt(temp[1]);
System.out.println(“person :”+ person +”game “+ game);
  }catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“Error of Input!”);
}
System.out.println(game,person);
                }

这样就行了 ,作用域的问题。

+1

为什么我的代码加上try catch之后反而编译不通过
10分
作用域问题person,game在try以外都访问不到, 你这样写

public static void main(String[] args) {   
        int person,game;
        try {   
            Scanner in = new Scanner(System.in);
            String ss = in.nextLine();
         
            String[] temp = ss.split(" ");
            person = Integer.parseInt(temp[0]);
            game = Integer.parseInt(temp[1]);
            System.out.println("person :"+ person +"game "+ game);
      }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Error of Input!");
        }
        System.out.println(game,person);
                }
为什么我的代码加上try catch之后反而编译不通过
System.out.println(game,person);
为什么我的代码加上try catch之后反而编译不通过
引用 1 楼 u012463264 的回复:

public static void main(String[] args) {
      int person=0;
      int game =0;
try {
Scanner in = new Scanner(System.in);
String ss = in.nextLine();

String[] temp = ss.split(” “);
 person = Integer.parseInt(temp[0]);
 game = Integer.parseInt(temp[1]);
System.out.println(“person :”+ person +”game “+ game);
  }catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“Error of Input!”);
}
System.out.println(game,person);
                }

这样就行了 ,作用域的问题。

太谢谢了

为什么我的代码加上try catch之后反而编译不通过
引用 4 楼 whos2002110 的回复:

作用域问题person,game在try以外都访问不到, 你这样写

public static void main(String[] args) {   
        int person,game;
        try {   
            Scanner in = new Scanner(System.in);
            String ss = in.nextLine();
         
            String[] temp = ss.split(" ");
            person = Integer.parseInt(temp[0]);
            game = Integer.parseInt(temp[1]);
            System.out.println("person :"+ person +"game "+ game);
      }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Error of Input!");
        }
        System.out.println(game,person);
                }

谢谢

为什么我的代码加上try catch之后反而编译不通过
引用 2 楼 rui888 的回复:

 System.out.println(game,person);  看看api  没有 2个 参数的。

其次 ,你 game,person 也访问不到 ,变量的作用范围 不一样 。

嗯嗯,写c写惯了,结果写成了这样

为什么我的代码加上try catch之后反而编译不通过
哥哥, 你的game和person定义在try里面,然后再外面使用,肯定不行啊

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么我的代码加上try catch之后反而编译不通过
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!