我不明白这个Head First Java练习

时间:2013-10-29 05:13:00

标签: java

我正在阅读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 个答案:

答案 0 :(得分: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;

m4aMix4个对象的数组。您可以使用.运算符

调用对象方法或属性(变量)
 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?

在程序结束时打印。它在counter数组中的索引Mix4打印1对象的m4a

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

答案 1 :(得分: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 of Mix4 Class objects. You can call object methods or properties using .运营商 来自Using Objects

  

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

objectReference.fieldName

System.out.println(count +“”+ m4a 1。counter); //什么是此打印以及何时打印?

来自 System.out.println()

  

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

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

答案 2 :(得分:0)

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

你有这个部分是正确的 现在在第一次迭代中循环while循环x = 0;

Mix4[] m4a = new Mix4[20];

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

m4a[x] = new Mix4();

在第一个iretartion中我们是doing m4a[0] = new Mix4();所以索引0处的元素被初始化。

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

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

  int counter = 0;

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

另请注意,counter是一个实例变量(不是局部变量),实例变量是默认值(对于int,它是0,对于String,它是null,依此类推......)

尝试使用这种基本的理解逐步调试代码。

答案 3 :(得分:0)

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

  1. 你在while循环中有System.out.println;这将导致NullPointerException,因为当编译器到达m4a[1].counter时,尚未创建m4a [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;
    }
    }