Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要帮助测试我的课程吗_Java_Loops - Fatal编程技术网

Java 需要帮助测试我的课程吗

Java 需要帮助测试我的课程吗,java,loops,Java,Loops,我的任务: Stix的游戏——类似于不久前在“幸存者,泰国”上玩的游戏——在简化版本中,不知何故看起来是这样的: 它是由两个玩家玩的(在《幸存者》中不止两个,但这里我们只处理两个)。 桌子上放着许多棍子(像火柴)。 如果桌上有那么多棍子,第一个玩家可以拿走1、2或3根棍子。 然后第二个玩家拿走1、2或3根棍子(如果可能的话),依此类推。 谁拿了最后一根棍子,谁就输了 这是我的班级: public class StixBoard { public int numbe

我的任务:

Stix的游戏——类似于不久前在“幸存者,泰国”上玩的游戏——在简化版本中,不知何故看起来是这样的:

它是由两个玩家玩的(在《幸存者》中不止两个,但这里我们只处理两个)。 桌子上放着许多棍子(像火柴)。 如果桌上有那么多棍子,第一个玩家可以拿走1、2或3根棍子。 然后第二个玩家拿走1、2或3根棍子(如果可能的话),依此类推。 谁拿了最后一根棍子,谁就输了

这是我的班级:

    public class StixBoard
    {
        public int number;

        public StixBoard(int number)
        {
        number = number;
        }

        public int getNumStix()
        {
        return number;
        }

        public boolean takeStix(int number)
        {
            int take = 1;
            while(take <= getNumStix())
            {
            takeStix(take);
            take++;
            }

            if(number >= 1 && number <= 3)
            {
            number = number - this.number;
                System.out.println("Number of sticks on board:" + number);
            return(true);
            }
        else
        System.out.println("Illegeal Move");
        return(false);
        }

        public boolean isGameOver()
        {
            if(number >=1)
            {
            return(true);
            }
            else
            return false;
        }

            public String toString()
        {
            return(getNumStix() + " Stix Remaining.");
        }
    }

This is my tester:

    public class StixGame
    {

        public static void main(String[] args)
        {

        StixBoard game1 = new StixBoard(6);
        System.out.println(game1.getNumStix());


        }

    }

Can someone tell my why game1 only returns 0?

*UPDATE*
Now that it constantly displays:

    6
    Illegeal Move
    false
    6

I've been playing around with it but can't figure out why =/

Program now looks like this:

public class StixBoard
{
    public int number;

    public StixBoard(int number)
    {
    this.number = number;
    }

    public int getNumStix()
    {
    return number;
    }

    public boolean takeStix(int number)
    {
        int take = 0;

        while(take != number && number <= 3 && number > 0)
        {
        number = this.number - take;
        take++;
        }

        if(this.number >= 1 && this.number <= 3)
        {
        number = number - this.number;
            System.out.println("Number of sticks on board:" + number);
        return(true);
        }
    else
    System.out.println("Illegeal Move");
    return(false);
    }

    public boolean isGameOver()
    {
        if(number >=1)
        {
        return(true);
        }
        else
        return false;
    }

        public String toString()
    {
        return(getNumStix() + " Stix Remaining.");
    }
}

当您在构造函数中使用
number=number
时,它实际上使用了来自输入的变量,因此您基本上是将其重置为自身。然后,当构造函数完成时,它就不在范围内了,所以该变量就消失了。您需要做的是重命名函数参数或使用
this.number=number

当您在构造函数中使用
number=number
时,它实际上使用了来自输入的变量,因此您基本上是将其重置为自身。然后,当构造函数完成时,它就不在范围内了,所以该变量就消失了。您需要做的是重命名函数参数或使用
this.number=number

您必须使用以下命令初始化类:

public StixBoard(int number)
{
this.number = number;
}

避免对构造函数中的变量使用相同的名称

您必须使用以下命令初始化类:

public StixBoard(int number)
{
this.number = number;
}

避免对构造函数中的变量使用相同的名称

构造函数将
编号
参数设置为自身:

public StixBoard(int number)
{
number = number;
}
这个.number的默认值保持为
0
。您需要使用
this.
前缀为实例变量指定参数值,以避免名称冲突:

this.number = number;

构造函数将
number
参数设置为自身:

public StixBoard(int number)
{
number = number;
}
这个.number的默认值保持为
0
。您需要使用
this.
前缀为实例变量指定参数值,以避免名称冲突:

this.number = number;

问题在于您的构造函数:

public StixBoard(int number) {
    number = number;
}
替换为:

public StixBoard(int number) {
    this.number = number;
}

如您所见,参数编号与字段编号之间存在阴影。因此,asignment将参数编号分配给它自己,而不是类字段。要删除阴影,必须使用关键字
this
来引用在当前线程中执行的对象字段。

问题在于构造函数:

public StixBoard(int number) {
    number = number;
}
替换为:

public StixBoard(int number) {
    this.number = number;
}

如您所见,参数编号与字段编号之间存在阴影。因此,asignment将参数编号分配给它自己,而不是类字段。要删除阴影,必须使用关键字
this
来引用在当前线程中执行的对象字段。

我刚刚编辑了答案。你必须感谢的那个人是岳源。我刚刚编辑了答案。你必须感谢的那个人是岳源。