Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/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_Variables_Hashset_Minesweeper - Fatal编程技术网

Java 扫雷艇散列集

Java 扫雷艇散列集,java,variables,hashset,minesweeper,Java,Variables,Hashset,Minesweeper,我正在写一个扫雷游戏,我正在尝试使用哈希集。我的问题是哈希集可以同时包含整数和字符串吗?我使用的hastset主要有字符串和一个整数。这能做到吗?当我编译它时,它给我一个错误,变量_minecont找不到,它不是字符串。这是我的代码: import java.util.Set; import java.util.HashSet; /** * Represents a single square in Minesweeper. * * @author Sophia Ali */

我正在写一个扫雷游戏,我正在尝试使用哈希集。我的问题是哈希集可以同时包含整数和字符串吗?我使用的hastset主要有字符串和一个整数。这能做到吗?当我编译它时,它给我一个错误,变量_minecont找不到,它不是字符串。这是我的代码:

    import java.util.Set;
import java.util.HashSet;

/**
 * Represents a single square in Minesweeper.
 * 
 * @author Sophia Ali
 */
public class MineSquare
{
    //Constants:
    // Fields:
    private  String  _shown;      // What a square is showing now
    private  boolean _mined;      // Square is mined or not
    private  boolean _flagged;    // Square is flagged or not
    private  boolean _questioned; // Square is question marked or not
    private  int     _minecount;  // Square's surrounding mine count
    private  boolean _opened;     // Player has opened this square or not
    private static final String[] VALID_SHOWN_TEXT = {"X", 0 < minecount && _minecount < 8, " ", "F", "?"};
    private HashSet<String> validShownTextSet = 
    new HashSet<String>(java.util.Arrays.asList(VALID_SHOWN_TEXT));

    // Constructors & Methods:

    /**
     * Default constructor
     * Sets _mined and _opened to false.
     */
    public MineSquare()
    {
        _mined = false;
        _opened = false;
        setShown(" ");
    }





    /**
     * Returns flagged status of square.
     * @return  _flagged    Flagged status
     */
    public boolean isFlagged() {
        return _flagged;
    }





    /**
     * Sets or unsets flag on a square.
     * @param   flagged    True or false (square is flagged or not)
     */
    public void setFlagged(boolean flagged, boolean opened) {
        _flagged = flagged;
        _opened = opened;

        // If Minesquare opened do nothing:
        if (opened == true)
        setShown(" ");

        // If flagged, square should show "F":
        if ( isFlagged() == true )
            setShown("F");
        else
            setShown(" ");            
    }


    public int getMinecount() {
        return _minecount;
    }





    public void setMinecount(int minecount) {
        _minecount = minecount;

        if ( 0 < minecount && minecount < 8 ) 
        {
       setShown("Error:" + minecount);

    }
}





    public boolean isMined() {
        return _mined;
    }





    public void setMined(boolean mined) {
        _mined = mined;


    }





    public boolean isOpened() {
        return _opened;
    }





    /**
     * Open a square.
     * (Once opened, a square can't be unopened.)
     */
    public void setOpened() {
        _opened = true;

        if ( isMined() == true )
            setShown("X");
        else if ( getMinecount() > 0 )
            setShown(_minecount + "");
        else // blank space for _minecount = 0
            setShown(" ");
    }





    public boolean isQuestioned() {
        return _questioned;
    }





    public void setQuestioned(boolean questioned, boolean opened) {
        _questioned = questioned;
        _opened = opened;
        // If Minesquare opened do nothing:
        if (opened == true)
        setShown(" ");

        // If Questioned, square should show "F":
        if ( isQuestioned() == true )
            setShown("F");
        else
            setShown(" ");  
    }





    public String getShown() {
        return _shown;
    }





    public void setShown(String shown) {
        _shown = shown;
        /*if (shown.equals("X")) {
            this.shown = shown;
        } else if (shown.equals( 0 < minecount && minecount < 8 )) {
            this.shown = shown;
        } else if (shown.equals(" ")) {
            this.shown = shown;
        } else if (shown.equals("F")) {
            this.shown = shown;
        } else if (shown.equals("?")) {
            this.shown = shown;
        } else {
            this.shown = "Error + shown";
        }
        */
       if (validShownTextSet.contains(shown)) {
           this.shown = shown;
        } else {
            this.shown = "Error" + shown;
        }
    }





    }
import java.util.Set;
导入java.util.HashSet;
/**
*代表扫雷舰上的一个正方形。
* 
*@作者索菲亚·阿里
*/
公共级雷场
{
//常数:
//字段:
私有字符串_show;//现在显示的是一个正方形
私有布尔值_mined;//是否已挖掘平方
私有布尔值_标记;//是否标记正方形
private boolean _problem;//正方形是否带有问号
private int _minecont;//广场周围的地雷计数
私有布尔值_opened;//玩家是否已打开此方块
私有静态最终字符串[]有效的\u显示\u文本={“X”,00)
设置显示(_地雷计数+);
else//用于_minecont=0的空格
设置显示(“”);
}
公共布尔值(){
返回被质疑;
}
public void setinvested(布尔值被询问,布尔值被打开){
_质疑的;
_打开=打开;
//如果Minesquare已打开,请不要执行任何操作:
如果(打开==真)
设置显示(“”);
//如有疑问,正方形应显示“F”:
如果(IsQuestion()==true)
设置显示(“F”);
其他的
设置显示(“”);
}
公共字符串getShowed(){
返回显示;
}
公共无效设置显示(显示字符串){
_显示=显示;
/*如果(显示为等于(“X”)){
this.show=show;
}否则如果(显示为等于(0
这一行

private static final String[] VALID_SHOWN_TEXT = {"X", 0 < minecount && _minecount < 8, " ", "F", "?"};

HashSet
可以同时包含整数和字符串,但在这里,看起来您可以简单地将所需内容转换为字符串数组,只有在使用
HashSet
时,才能将字符串数组转换为
HashSet

,既然
Integer
String
没有共同的超类,那么这一行应该做什么呢?私有静态最终字符串[]有效的\u显示\u文本={“X”,0// or a working boolean expression here. ..., String.valueOf(0 < minecount && _minecount < 8), ...