我调用正确了吗?
我得到了一段代码(QuestionTwo 类)。 在类 Q2 的新创建对象上调用方法 mQ2 后,我被要求陈述 a、b 和 c 的值。
我的 main.java 文件
package openuniversity;
public class Main
{
public static void main(String[] args)
{
QuestionTwo qt = new QuestionTwo();
qt.mQ2();
}
}
我的 QuestionTwo.java 类文件:
package openuniversity;
public class QuestionTwo
{
int a;
int b = 1;
public void mQ2()
{
{
int c;
int a = 2;
c = a;
}
{
int c;
int a;
c = 3;
a = 4;
}
a++;
}
}
我到达:
a: 1
b: 1
c: 3
注意我还可以选择“未定义”作为答案? 那么它会是 1, 1, undefined 因为 c 不存在于代码块之外吗?
问题:
研究以下代码,然后从下面的下拉列表中选择选项在类 Q2 的新创建对象上调用方法 mQ2 一次后,关于 a、b 和 c 的值是正确的。请注意,您为 a、b 和 c 选择的答案可能会或可能不会彼此不同。
public class Q2
{
int a;
int b = 1;
public void mQ2()
{
{
int c;
int a = 2;
c = a;
}
{
int c;
int a;
c = 3;
a = 4;
System.out.println("c: " + c); //correct place?
}
a++;
}
System.out.println("a: " + a + "b: " + b); // correct place?
}
I have been given a piece of code (the class QuestionTwo).
I am asked to state the values of a, b, and c after method mQ2 is invoked on a newly created object of class Q2.
My main.java file
package openuniversity;
public class Main
{
public static void main(String[] args)
{
QuestionTwo qt = new QuestionTwo();
qt.mQ2();
}
}
My QuestionTwo.java class file:
package openuniversity;
public class QuestionTwo
{
int a;
int b = 1;
public void mQ2()
{
{
int c;
int a = 2;
c = a;
}
{
int c;
int a;
c = 3;
a = 4;
}
a++;
}
}
I arrived at:
a: 1
b: 1
c: 3
Note I can also select 'undefined' as an answer?
So would it be 1, 1, undefined as c does not exist outside of the codeblock?
The question:
Study the following code and then select the options from the drop-down lists below that are correct about the values of a, b and c after the method mQ2 is invoked once on a newly created object of class Q2. Note that the answers you choose for a, b and c may or may not be different from each other.
public class Q2
{
int a;
int b = 1;
public void mQ2()
{
{
int c;
int a = 2;
c = a;
}
{
int c;
int a;
c = 3;
a = 4;
System.out.println("c: " + c); //correct place?
}
a++;
}
System.out.println("a: " + a + "b: " + b); // correct place?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您在调用 mQ2 后首先使用 System.out.println() 打印出所有值,然后在您的脑海中逐步执行代码以尝试找出这些值的原因 。请记住,声明的任何变量仅在作用域内可见(为简单起见,
{...}
),但这些变量可以与其他变量具有相同的名称,因此即使它们是相同的,它们也可能看起来相同不是。我想特别指出 c 在该方法之外不存在。
I'd suggest you first print out all the values using System.out.println() after calling mQ2, then step through the code in your mind to try to work out why the values are what they are. Remember that any variable declared is only visible within the scope (
{...}
s for simplicity), but these variables can have the same name as other variables so they might look like the same even if they're not.I'd like to particularly point out that c does not exist outside that method.
由于这是家庭作业,我将把我的答案限制在几个提示上。
mQ2()
后打印出变量来验证您的解决方案(提示:您可以使用System.println()
)。a
,b
,尤其是c 你被问到)。
Since this is homework, I'll restrict my answer to a couple of pointers.
mQ2()
(hint: you could useSystem.println()
for that).a
,b
and especiallyc
you're being asked about).