一道java选择题
importjavax.swing.*;publicclassShowErrors{publicstaticvoidmain(String[]args){inti;int...
import javax.swing.*;public class ShowErrors
{public static void main(String[] args)
{int i;int j;
String s = JOptionPane.showInputDialog(null, "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE);
j = Integer.parseInt(s);
i = (i + 4);
}
}
1、 The program cannot compile because j is not initialized.
2、 The program cannot compile because i does not have an initial value when it is used in i = i + 4;
3、 The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
4、 The program compiles and runs fine.
答案选2,但是int不是默认i=0吗?为什么2选项还说i没初始化呢 展开
{public static void main(String[] args)
{int i;int j;
String s = JOptionPane.showInputDialog(null, "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE);
j = Integer.parseInt(s);
i = (i + 4);
}
}
1、 The program cannot compile because j is not initialized.
2、 The program cannot compile because i does not have an initial value when it is used in i = i + 4;
3、 The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
4、 The program compiles and runs fine.
答案选2,但是int不是默认i=0吗?为什么2选项还说i没初始化呢 展开
4个回答
展开全部
int的全局变量,会初始化默认为0;
方法内的成员变量需要赋初值,否则不仅不能运行,就连编译都不会通过。
方法内的成员变量需要赋初值,否则不仅不能运行,就连编译都不会通过。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
作为一个局部变量,它必须被显示的初始化,否则不能使用……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大家都说了 我也就不说了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询