出错的原因:
eclipse中,当声明了变量,没有进行初始化,然后在方法中引用该变量的时候,就会报出如下错误:
The local variable XXX may not have been initialized意思是该变量没有进行初始化,解决这个错误就是赋初值。
但是
package 会查;
import java.util.*;public class hanshu1 {public static void main(String args[]){Scanner scan = new Scanner(System.in);int y,x;x = scan.nextInt();y = x * x;if(x == 0) {y = 0;}if(x < 0) {y = x * x * (-1);}System.out.println(y);}
}
这个是可以的
package 会查;
import java.util.*;public class hanshu1 {public static void main(String args[]){Scanner scan = new Scanner(System.in);int y,x;x = scan.nextInt();if(x == 0) {y = x * x;}if(x == 0) {y = 0;}if(x < 0) {y = x * x * (-1);}System.out.println(y);}
}
原因是if判断会阻隔