我调用正确了吗?

发布于 2024-12-11 22:19:17 字数 1310 浏览 0 评论 0原文

我得到了一段代码(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-12-18 22:19:18

我建议您在调用 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.

云淡月浅 2024-12-18 22:19:17

由于这是家庭作业,我将把我的答案限制在几个提示上。

  1. 您可以通过在调用 mQ2() 后打印出变量来验证您的解决方案(提示:您可以使用 System.println())。
  2. 这要么是一个棘手的问题,要么部分定义不明确(提示:想想whichab,尤其是c 你被问到)。

Since this is homework, I'll restrict my answer to a couple of pointers.

  1. You can verify your solution by printing out the variables after calling mQ2() (hint: you could use System.println() for that).
  2. This is either a trick question or is partially ill-defined (hint: think about which a, b and especially c you're being asked about).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文