【问题标题】:How do I set a variable properly to be tested in a boolean?如何正确设置变量以在布尔值中进行测试?
【发布时间】:2017-06-30 22:38:24
【问题描述】:

在我的课堂上,我们分配了一个名为 Battleship 的 CodeHS 作业。我被困在第 7 部分的第 2 部分,即 Location 类。练习如下:

下一个要编写的类是 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;
}

}

虽然如果我在其他地方出现错误,我真的不会感到惊讶,但我的主要问题是 setShip 布尔方法。我应该如何将其设置为 true(该位置有一艘船)或 false(没有船)。我所拥有的是我最好的猜测,但只有在“射击”之后进行测试时才是正确的。而且我认为该练习希望在此之前对其进行测试。

【问题讨论】:

  • 1) 位置应该是Point (x,y) 吗? 2) 字段 val 应更改为表示 hasShip 3) 如果您使用 setter/getter 功能,您会看到您应该设置并获取 hasShip 值 4) 考虑 public boolean checkHit () {return localtion == HIT);}

标签: java methods initialization boolean logic


【解决方案1】:

你有一个私有的布尔值,你用 hasShip() 返回。

您的 setShip(val) 方法应该使用 val 设置类变量,而不是根据统计数据进行计算。

这只是一个简单的setter方法。

编辑:

就像

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

【讨论】:

    【解决方案2】:

    首先:不要更改从练习中获得的方法签名(例如,构造函数不应该有参数,markHitmarkMiss 方法也是如此。

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

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

    您所要做的就是更改markMissmarkHit方法上的状态变量并相应地调用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;
    }
    

    【讨论】:

      【解决方案3】:

      感谢各位的帮助。使用提供的反馈,我将课程固定为以下内容,它现在可以工作了。

      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;
          }
      }
      

      【讨论】:

      • 这看起来更好,但请注意它仅起作用,因为如果未初始化,默认情况下 int 为 0。此外,您的方法不必要大,您可以只返回 status == UNGUESSED 而不是 if(status == UNGUESSED) return true;否则返回假;它做同样的事情。
      猜你喜欢
      • 2021-11-29
      • 2012-06-20
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多