在我的类(class)中,我们被分配了一项名为“战舰”的 CodeHS 作业。我被困在 7 部分的第 2 部分,位置类。练习如下:
下一个要编写的类是 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 Boolean 方法。我应该如何将其设置为 true(该位置有一艘船)或 false(没有船)。我所得到的是我最好的猜测,但只有在“射击”后进行测试时才是正确的。我认为演习希望在此之前对其进行测试。
最佳答案
谢谢大家的帮助。根据提供的反馈,我将类(class)修复为以下内容,现在它可以工作了。
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;
}
}
关于java - 如何正确设置变量以在 boolean 值中进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195666/