-1

我正在阅读 Head First Java,一个练习让我有点困惑。练习的原始说明无关紧要,但重点是能够在不编译代码并运行它的情况下解决它,这只会吐出答案。我很困惑,正在尝试播放调试器并逐步执行每一行代码,看看发生了什么。我已将我的评论添加到代码中以确保我理解它。只需要帮助了解例如在特定点的计数是多少等。这是原始代码,我自己添加了一行,我已经注意到了。我会注意一些我不太理解的行。

**更新:所以我对我的代码给出了最好的理解。我对某些行的问题在评论中。如果有人可以逐步了解所发生的事情,那将会更容易理解。提前感谢大家的帮助。我是 StackOverFlow 的新手,所以希望这是提出问题的正确方法。

public class Mix4
{
    int counter = 0;    //This is setting the variable counter to 0.

    public static void main (String[] args)
    {
        int count = 0;    //This is setting the variable count to 0.

        Mix4[] m4a = new Mix4[20];//This is initializing an array of 20 m4a objects to  null.

        int x = 0;        //This is setting the variable x to 0;

        while ( x < 9 )
        {
            m4a[x] = new Mix4(); //This actually creates the m4a object at array index 0.

            m4a[x].counter = m4a[x].counter + 1;
            //This line is very confusing. How can you use a dot operator on a variable?
            //I am saying variable because as stated above there is a int counter = 0;

            count = count + 1; //This increments the variable count. Why do this though?

            count = count + m4a[x].maybeNew(x);

            //The count variable again is being implemented but this time it calls the
            // maybeNew method and it is passing a 0 as as the argument? Why do this?

            x = x + 1; // x is being incremented. 

            System.out.println(count + " " + m4a[1].counter); 
            //What is this printing and when does this print?
        }

    public int maybeNew(int index)
    {
        if (index < 5)
        {
            Mix4 m4 = new Mix4(); //Creating a new object called m4.

            m4.counter = m4.counter + 1;
            //Same question about this from the code of line stated above using dot
            //operators on variables.

            return 1; //Where does 1 be returned to? I thought you can only have one
                      //return statement per method?
        }
        return 0; // I thought only 1 return statement? I have no idea what these return
                  // statements are doing
    }

    }
}
4

4 回答 4

0

上面显示的代码无法编译,原因有两个:

  1. System.out.println在while循环里面;这将导致 NullPointerException,因为当编译器到达m4a[1].counterm4a[1] 对象时尚未创建。仅创建了 m4a[0]。或者,您可以将此行保留在原处,但将其更改为m4a[0].counter
  2. 你有maybeNew(int index)里面的方法声明main;这很可能只是你的大括号的错误,但只是为了让你知道你不能在另一个方法中声明一个方法;main是一种方法,因此您必须maybeNew在其外部声明,但从内部调用它main

     public class Mix4 {
     int counter = 0;
     public static void main (String[] args) {
     int count = 0;
     Mix4[] m4a = new Mix4[20];
     int x = 0;
    
     while (x < 9) {
        m4a[x] = new Mix4();
        m4a[x].counter = m4a[x].counter + 1;
        count = count + 1;
        count = count + m4a[x].maybeNew(x);
        x = x + 1;
       // System.out.println(count + " " + m4a[0].counter);
    }
     System.out.println(count + " " + m4a[1].counter);
    }
        public int maybeNew(int index)
    {
     if (index < 5)
    {
        Mix4 m4 = new Mix4();
        m4.counter = m4.counter + 1;
        return 1;
    }
     return 0;
    }
    }
    
于 2015-01-26T12:32:09.757 回答
0
 m4a[0].counter = m4a[x].counter + 1;
 //This line is very confusing. How can you use a dot operator on a variable?
 //I am saying variable because as stated above there is a int counter = 0;

m4a是一个Mix4对象数组。.您可以使用运算符调用对象方法或属性(变量)

 count = count + m4a[x].maybeNew(x);
 //The count variable again is being implemented but this time it calls the
 // maybeNew method and it is passing a 0 as as the argument? Why do this?

maybeNew()返回一个int. 您正试图通过maybeNew(x)返回的任何数字来增加计数。 x是 的值m4a[0],如果x = 0

System.out.println(count + " " + m4a[1].counter); 
//What is this printing and when does this print?

这将在您的程序结束时打印。它在数组中的索引处打印对象的counterMix41m4a

return 1; //Where does 1 be returned to? I thought you can only have one
                  //return statement per method?
    }
    return 0; // I thought only 1 return statement? I have no idea what these return
              // statements are doing

首先,一个方法可能会也可能不会返回一个值。在你的方法中,你希望它返回一个int. 因此,当您在此处调用它时count = count + m4a[x].maybeNew(x);,它就像是在说,无论maybeNew(x)返回什么数字,都将其添加到计数中。

您的第一个return 1是在条件语句中。如果条件满足, return 1, 否则return 0

于 2013-10-29T05:24:32.243 回答
0

1返回到哪里?我以为每个方法只能有一个返回语句?

您的函数中的一个分支可能有一个return语句,它不限于return每个函数只有一个语句,它应该只有return一个逻辑路径的一个语句。喜欢 :

public int DoStuff(Foo foo) {
   if (foo == null) return 0;
   ...
   return 1; // any thing 
 }

m4a[0].counter = m4a[x].counter + 1;
//这一行非常混乱。如何在变量上使用点运算符?//我说的是变量,因为如上所述有一个 int counter = 0;

m4a is an array ofMix4运算Class objects. You can call object methods or properties using.
使用对象

对象类之外的代码必须使用对象引用或表达式,后跟点 (.) 运算符,后跟简单的字段名称,如下所示:

objectReference.fieldName

System.out.println(count + " " + m4a 1 .counter); //这是什么打印,什么时候打印?

System.out.println()

打印一个对象,然后终止该行。此方法首先调用 String.valueOf(x) 以获取打印对象的字符串值,然后表现得好像它调用 print(String) 然后调用 println()。

它将在 m4a 数组中的索引 1 处打印 Mix4 对象的count值和counter值。

于 2013-10-29T05:24:56.937 回答
0
int x = 0;        //This is setting the variable x to 0;

你现在在第一次迭代 x = 0; 的 while 循环中得到了这部分。

Mix4[] m4a = new Mix4[20];

正如您正确猜测的那样,这再次只是定义一个数组。简单地说,它是一组 20 个 Mix 类型的引用,它们将指向 Mix 类型的实际 Object。现在您必须将这些对象分配给我们在 while 循环中所做的引用。

m4a[x] = new Mix4();

在第 1 次迭代中,我们将doing m4a[0] = new Mix4();初始化索引 0 处的元素。

m4a[0].counter = m4a[x].counter 这里我们只是访问实际对象的计数器并为其赋值。

如何在变量上使用点运算符?首先,如果所有 m4a[0] 都已初始化。接下来你需要知道的是访问修改器。如果你看声明

  int counter = 0;

没有指定访问修饰符,这意味着它具有默认访问修饰符。现在对于变量的默认访问修饰符可见性在同一个类和同一个包中(不需要 getter/setter 方法)。

另请注意,计数器是一个实例变量(不是局部变量)并且实例变量被分配了默认值(对于 int 它是 0,对于 String 它是 null 等等......)

试着带着这个基本的了解一步步调试代码。

于 2013-10-29T05:27:52.087 回答