Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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_Methods_Initialization_Boolean_Logic - Fatal编程技术网

Java 如何正确设置要在布尔值中测试的变量?

Java 如何正确设置要在布尔值中测试的变量?,java,methods,initialization,boolean,logic,Java,Methods,Initialization,Boolean,Logic,在我的课堂上,我们被分配了一个代号为“战舰”的任务。我被困在第二部分的7,位置类。行动如下: public class Location { private int location; private int hit; private int miss; private boolean val; private int status; //Implement the Location class here public static fin

在我的课堂上,我们被分配了一个代号为“战舰”的任务。我被困在第二部分的7,位置类。行动如下:

public class Location
{
    private int location;
    private int hit;
    private int miss;
    private boolean val;
    private int status;
    //Implement the Location class here
    public static final int UNGUESSED = 0;
    public static final int HIT = 1;
    public static final int MISSED = 2;

    // Location constructor. 
    public Location(int location){
        this.location = location;
    }

// Was this Location a hit?
public boolean checkHit(){
    if(location == HIT)
    return true;
    return false;
}

// Was this location a miss?
public boolean checkMiss(){
    if(location == MISSED)
    return true;
    return false;
}

// Was this location unguessed?
public boolean isUnguessed(){
    if(location == UNGUESSED)
    return true;
    return false;
}

// Mark this location a hit.
public void markHit(int hit){
    this.hit = hit;
}

// Mark this location a miss.
public void markMiss(int miss){
    this.miss = miss;
}

// Return whether or not this location has a ship.
public boolean hasShip(){
    return val;
}

    // Set the value of whether this location has a ship.
   public void setShip(boolean val){
    if(status == HIT)
    val = true;
    else 
    val = false;
}

// Set the status of this Location.
public void setStatus(int status){
    this.status = status;
}

// Get the status of this Location.
public int getStatus(){
    if(status == HIT)
    return HIT;
    else if (status == MISSED)
    return MISSED;
    else if(status == UNGUESSED)
    return UNGUESSED;
}

}
下一个要编写的类是Location.java文件。Location类存储一个栅格位置的信息

位置有两个定义属性

1) 这里有船吗

2) 该位置的状态如何

这个位置的状态将是该位置是否未使用、我们是否命中或未命中

public static final int UNGUESSED = 0;
public static final int HIT = 1;
public static final int MISSED = 2;
这些是需要为Location类实现的方法

// Location constructor. 
public Location()

// Was this Location a hit?
public boolean checkHit()

// Was this location a miss?
public boolean checkMiss()

// Was this location unguessed?
public boolean isUnguessed()

// Mark this location a hit.
public void markHit()

// Mark this location a miss.
public void markMiss()

// Return whether or not this location has a ship.
public boolean hasShip()

// Set the value of whether this location has a ship.
public void setShip(boolean val)

// Set the status of this Location.
public void setStatus(int status)

// Get the status of this Location.
public int getStatus()
我的工作如下:

public class Location
{
    private int location;
    private int hit;
    private int miss;
    private boolean val;
    private int status;
    //Implement the Location class here
    public static final int UNGUESSED = 0;
    public static final int HIT = 1;
    public static final int MISSED = 2;

    // Location constructor. 
    public Location(int location){
        this.location = location;
    }

// Was this Location a hit?
public boolean checkHit(){
    if(location == HIT)
    return true;
    return false;
}

// Was this location a miss?
public boolean checkMiss(){
    if(location == MISSED)
    return true;
    return false;
}

// Was this location unguessed?
public boolean isUnguessed(){
    if(location == UNGUESSED)
    return true;
    return false;
}

// Mark this location a hit.
public void markHit(int hit){
    this.hit = hit;
}

// Mark this location a miss.
public void markMiss(int miss){
    this.miss = miss;
}

// Return whether or not this location has a ship.
public boolean hasShip(){
    return val;
}

    // Set the value of whether this location has a ship.
   public void setShip(boolean val){
    if(status == HIT)
    val = true;
    else 
    val = false;
}

// Set the status of this Location.
public void setStatus(int status){
    this.status = status;
}

// Get the status of this Location.
public int getStatus(){
    if(status == HIT)
    return HIT;
    else if (status == MISSED)
    return MISSED;
    else if(status == UNGUESSED)
    return UNGUESSED;
}

}

虽然我真的不会感到惊讶,如果我在其他地方有错误,我的主要问题是设置片布尔方法。我应该如何将其设置为真(有一艘船处于该位置)或假(没有船)。我所做的是我最好的猜测,但只有在“射击”后进行测试,这才是真的。我认为这个练习希望在这之前对它进行测试。

您有一个私有布尔值,它将与haship()一起返回

您的setShip(val)方法应该使用val设置类变量,而不是基于stats计算

这只是一个简单的setter方法

编辑:

就像

// Mark this location a hit.
public void markHit(int hit){
    this.hit = hit;
}

第一:不要更改从练习中获得的方法签名(例如,构造函数不应该有参数,
markHit
markMiss
方法也是如此)

您不需要将
int miss
int hit
保存在两个不同的变量中,因为不能同时命中和错过一个位置,因此我们使用单个变量
status

我很确定你的问题1和问题2(或多或少)是一样的,因为如果命中,就会有一艘船,反之亦然,所以你只会有一个属性
int status=0
,因此每个位置默认都是
未选中的
,你可以从那里读取所有内容

然后,您只需更改
markMiss
markHit
方法上的状态变量,并相应地调用
setShip

代码示例:

public void markMiss() {
    this.status = MISSED;
    setShip(false);
}

public void markHit() {
    this.status = HIT;
    setShip(true);
}

public boolean checkMiss() {
    return this.status == MISSED;
}

public boolean checkHit() {
    return this.status == HIT;
}

public boolean setShip(boolean val) {
    this.hasAShip = val;
}

public boolean hasShip() {
    return this.hasAShip;
}

感谢大家的帮助。根据提供的反馈,我将课程改为以下内容,现在可以使用了

public class Location
{
    private int status;
    private boolean ship;
    //Implement the Location class here
    public static final int UNGUESSED = 0;
    public static final int HIT = 1;
    public static final int MISSED = 2;

    // Location constructor. 
    public Location(){
        status = UNGUESSED;
        ship = false;
    }

    // Was this Location a hit?
    public boolean checkHit(){
        if(status == HIT)
            return true;
        return false;
    }

    // Was this location a miss?
    public boolean checkMiss(){
        if(status == MISSED)
            return true;
        return false;
    }

    // Was this location unguessed?
    public boolean isUnguessed(){
        if(status == UNGUESSED)
            return true;
        return false;
    }

    // Mark this location a hit.
    public void markHit(){
        status = HIT;
    }

    // Mark this location a miss.
    public void markMiss(){
        status = MISSED;
    }

    // Return whether or not this location has a ship.
    public boolean hasShip(){
        return ship;
    }

    // Set the value of whether this location has a ship.
    public void setShip(boolean val){
        ship = val;
    }

    // Set the status of this Location.
    public void setStatus(int status){
        this.status = status;
    }

    // Get the status of this Location.
    public int getStatus(){
        return status;
    }
}

1) 位置是否应该是一个
(x,y)?2)字段<代码> Val< /代码>应更改为“代码> >代码< /代码> 3)如果您使用SETTHER/GETTER功能,您将看到,只需设置并获得<代码> HaspAs/Cuff>值4)考虑<代码>公有BooLoeCalkHIT(){RealLoalTys=命中);}看起来更好,尽管如此,请注意它只起作用,因为如果未初始化,int默认值为0。此外,您的方法不必要太大,您可以只返回status==unguesed,而不是if(status==unguesed)返回true;否则返回false;它做着完全相同的事情。