Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 布尔函数总是返回false_Java - Fatal编程技术网

Java 布尔函数总是返回false

Java 布尔函数总是返回false,java,Java,我的程序创建了一个4x4的JButton网格,所有这些按钮都有随机字母作为文本值。这些JButton存储在名为diceLayout[][]的2D数组中。此函数传递由用户提交的由上述字母组成的单词 它需要检查周围的字母,看看它们的字母是否等于单词的下一个字母。如果单词完全连接,函数将返回true。如果不是,函数将返回false 但是,由于某些原因,该函数只会返回false。我已经附上下面的功能,如果你需要更多的程序让我知道。谢谢 private boolean checkWordConnected

我的程序创建了一个4x4的JButton网格,所有这些按钮都有随机字母作为文本值。这些JButton存储在名为
diceLayout[][]
的2D数组中。此函数传递由用户提交的由上述字母组成的单词

它需要检查周围的字母,看看它们的字母是否等于单词的下一个字母。如果单词完全连接,函数将返回
true
。如果不是,函数将返回
false

但是,由于某些原因,该函数只会返回
false
。我已经附上下面的功能,如果你需要更多的程序让我知道。谢谢

private boolean checkWordConnected(String word) {
    word = word.toLowerCase();
    String letters[] = word.split("");
    boolean conn = false;
    outerloop: for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (letters[0].equals(diceLayout[i][j].getText().toLowerCase()) 
                    && diceLayout[i][j].getIsClicked()) {
                pos[0] = i;
                pos[1] = j;
                break outerloop;
            } else if (i == 3 && j == 3) {
                return false;
            }
        }
    }
    for (int a = 1; a < letters.length - 1; a++) {
        loop: for (int i = -1; i < 2; i++) {
            for (int j = -1; j < 2; j++) {
                int checkPos[] = { pos[0] + i, pos[1] + j };
                if (checkPos[0] < 4 && checkPos[0] > 0 && checkPos[1] > 0 && checkPos[1] < 4) {
                    if (letters[a].equals(diceLayout[checkPos[0]][checkPos[1]].getText().toLowerCase())
                            && diceLayout[checkPos[0]][checkPos[1]].getIsClicked()) {
                        conn = true;
                        System.out.println(conn);
                        pos[0] = checkPos[0];
                        pos[1] = checkPos[1];
                        break loop;
                    }
                }
            }
        }
    }
    return conn;
}
private boolean checkWordConnected(字符串字){
word=word.toLowerCase();
字符串字母[]=单词。拆分(“”);
布尔连接=假;
outerloop:for(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
if(字母[0].equals(布局[i][j].getText().toLowerCase())
&&diceLayout[i][j].getIsClicked()){
pos[0]=i;
pos[1]=j;
断开外环;
}else如果(i==3&&j==3){
返回false;
}
}
}
for(int a=1;a0&&checkPos[1]>0&&checkPos[1]<4){
if(字母[a].equals(布局[checkPos[0]][checkPos[1]]].getText().toLowerCase())
&&diceLayout[checkPos[0]][checkPos[1]]。getIsClicked(){
conn=真;
系统输出打印LN(conn);
pos[0]=checkPos[0];
位置[1]=检查位置[1];
断环;
}
}
}
}
}
返回连接;
}
输入和输出示例:

当此函数的值(带传入的单词)返回时,输出栏中显示“false”

按下的JButtons文本添加到字符串变量createWord中,在submitButton ActionListener中,它接受该单词并将其传递给函数checkValidWord,该函数再次将值传递给checkWordConnected和其他根据条件测试该单词的函数

控制器类的完整代码:

 /*
  * To change this license header, choose License Headers in Project Properties.
  * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */
  package Controller;

  import Model.Die;
  import Model.Scoreboard;
  import View.GameWindow;
  import View.HelpWindow;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import javax.swing.JButton;

public class Controller {

private GameWindow view;
private static int pos[] = new int[2];
private Die diceLayout[][];
private String createdWord;

public Controller(){

    view = new GameWindow();
    addHelpListener();
    addSubmitListener();
    diceLayout = view.getDice();
    addDiceListeners();
}

private void submitWord(String word){
    boolean valid = checkValidWord(word);
    System.out.println("Word Validity: " + valid);
    if(valid=true){
        System.out.println("The word ‘"+word+"‘ is valid.");
        setDieClicked(false);
        createdWord="";
    }else{System.out.println("The word ‘"+word+"‘ is not valid.");}
}

private boolean checkValidWord(String word){
    boolean validSpell = validWordDic(word);
    boolean connected = checkWordConnected(word);
    System.out.println("Connected?: "+validSpell);
    if(validSpell&&connected&&word.length()>3){
        return true;
    }else{
        return false;
    }
}

private static boolean validWordDic(String word){
    word=word.toLowerCase();
    try {
        BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\sambe\\Desktop\\IPT\\BoggleGame\\res\\dictionary.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            if (str == word) {
                return true;
            }
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

private boolean checkWordConnected(String word){
    word = word.toLowerCase();
    String letters[] = word.split("");
    boolean conn = false;
    outerloop:
    for(int i = 0; i<4; i++){
        for(int j = 0; j<4; j++){
            if(letters[0].equals(diceLayout[i][j].getText().toLowerCase())&&diceLayout[i][j].getIsClicked()){
                pos[0]=i;
                pos[1]=j;
                break outerloop;
            }else if(i==3&&j==3){
                return false;
            }
        }
    }
    for(int a = 1; a<letters.length-1; a++){
        loop:
        for(int i = -1; i < 2; i++){
            for(int j = -1; j < 2; j++){
                int checkPos[] = {pos[0]+i,pos[1]+j};
                if(checkPos[0]<4&&checkPos[0]>0&&checkPos[1]>0&&checkPos[1]<4){
                    if(letters[a].equals(diceLayout[checkPos[0]][checkPos[1]].getText().toLowerCase())&&diceLayout[checkPos[0]][checkPos[1]].getIsClicked()){
                        conn=true;
                        System.out.println(conn);
                        pos[0]=checkPos[0];
                        pos[1]=checkPos[1];
                        break loop;
                    }
                }
            }
        }
    }
    return conn;
}

private void addHelpListener(){
    view.getHelpButton().addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) { 
            HelpWindow helpWin = new HelpWindow();
            System.out.println("done");
        } 
    });
}

private void addSubmitListener(){
    view.getSubmitButton().addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("done");
            if(!"".equals(view.getSubmitField().getText())){
                submitWord(view.getSubmitField().getText());
            }else{
                submitWord(createdWord);
            }
            view.getSubmitField().setText("");
            setDieColourNull();
        } 
    });
}

private void addDiceListeners(){
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            Die die = diceLayout[i][j];
            die.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    if(!die.getIsClicked()){
                        die.setClicked(true);
                        if(createdWord==null){
                            createdWord=die.getText();
                        }else{
                            createdWord=createdWord+die.getText();
                        }
                        System.out.println(createdWord); 
                    }
                }
            });
        }
    }
}

private void setDieColourNull(){
    for(int i =0; i<4; i++){
        for(int j = 0; j<4; j++){
            diceLayout[i][j].setBackground(new JButton().getBackground());
        }
    }
}

private void setDieClicked(boolean bool){
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            diceLayout[i][j].setClicked(bool);
        }
    }
}

private int scoreWord(String word){
    int score=0;
    if(word.length()==3){
        score=1;
    }else if(word.length()==4){
        score=1;
    }else if(word.length()==5){
        score=2;
    }else if(word.length()==6){
        score=3;
    }else if(word.length()==7){
        score=5;
    }else if(word.length()>=8){
        score=11;
    }
    return score;
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装控制器;
进口模具;
导入模型。记分板;
导入View.GameWindow;
导入View.HelpWindow;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入javax.swing.JButton;
公共类控制器{
私人游戏窗口视图;
私有静态整数pos[]=新整数[2];
专用模具布局[];
私有字符串createdWord;
公共控制员(){
视图=新游戏窗口();
addHelpListener();
addSubmitListener();
diceLayout=view.getDice();
addDiceListeners();
}
私有void submitWord(字符串字){
布尔有效=校验有效字(字);
System.out.println(“单词有效性:“+valid”);
if(valid=true){
System.out.println(“单词'”+word+“'是有效的。”);
SetDieClacked(假);
createdWord=“”;
}else{System.out.println(“单词'+word+''无效。”);}
}
专用布尔校验字(字符串字){
布尔值validSpell=validWordDic(字);
布尔连接=检查字连接(字);
System.out.println(“已连接?:”+validSpell);
if(validSpell&&connected&&word.length()>3){
返回true;
}否则{
返回false;
}
}
私有静态布尔值validWordDic(字符串字){
word=word.toLowerCase();
试一试{
BufferedReader in=new BufferedReader(新文件阅读器(“C:\\Users\\sambe\\Desktop\\IPT\\BoggleGame\\res\\dictionary.txt”);
字符串str;
而((str=in.readLine())!=null){
if(str==word){
返回true;
}
}
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回false;
}
专用布尔校验字连接(字符串字){
word=word.toLowerCase();
字符串字母[]=单词。拆分(“”);
布尔连接=假;
外部环路:

对于(int i=0;i让我给你看一个例子,看看如何通过检查是否在当前字母附近找到下一个字母来反向搜索。我简化了周围的程序,只使用了静态数组
diceLayout
而不是按钮。为了向你展示算法的工作原理,我还添加了一些无用的
System.out.println

public class Dices {

    private static String[][] diceLayout = new String[4][4];   // instead of the button array a static String array

    public static void main(String[] args) {

        diceLayout[0] = new String[]{"a", "b", "c", "d"};
        diceLayout[1] = new String[]{"e", "f", "g", "h"};
        diceLayout[2] = new String[]{"i", "j", "k", "l"};
        diceLayout[3] = new String[]{"m", "n", "o", "p"};

        String word = "bfkl";  // simple test
        System.out.println(checkWordConnected(word));
    }

    private static boolean checkWordConnected(String word) {
        word = word.toLowerCase();
        String[] letters = word.split("");

        int[] nextPosition = getFirstPosition(letters[0]);    // nextPosition will always hold the i and j indices of the letter you are looking for
        if (nextPosition != null) {                           // if it is not null, then the very first letter of the word is in the array
            System.out.println("found " + letters[0]+ " at " + Arrays.toString(nextPosition));
        } else {
            return false;                                     // if it is null, then we don't even have the first letter and the result is false
        }
        for (int i = 1; i < letters.length; i++) {            // now let's check the other letters
            nextPosition = getNextPosition(letters[i], nextPosition[0], nextPosition[1]);
            if (nextPosition != null) {
                System.out.println("found " + letters[i] + " at " + Arrays.toString(nextPosition));                                  // we continue when we found our next position
            } else {
                return false;                                // otherwise the word is not complete
            }
        }
        return true;
    }

    // this is a helper method to find the first i and j index (the first position)
    // it is only called once and when it finds the indices of the very first letter,
    // it will return these indices as an int array. If the letter is not in the 
    // array, our position is null (the resulting int[] will be null)
    private static int[] getFirstPosition(String letter) {
        for (int i = 0; i < diceLayout.length; i++) {
            for (int j = 0; j < diceLayout[i].length; j++) {
                if (diceLayout[i][j].toLowerCase().equals(letter)) {
                    return new int[]{i, j};
                }
            }
        }
        return null;  
    }

    // this will find the index of the next letter, if it is near the current index
    // meaning, for every i we check if the letter is somewhere in between 
    // i-1 and i+1 
    // and for every j we check if the letter is in between j-1 and j+1
    private static int[] getNextPosition(String letter, int i, int j) {
        for (int k = i - 1; k <= i + 1; k++) {
            for (int l = j - 1; l <= j + 1; l++) {
                if (check(letter, k, l)) {
                    return new int[]{k, l};
                }
            }
        }
        return null;
    }

    // thats the actual method where we check if the letter is at the
    // indicated index. But first we have to make sure that the index is valid
    // and does not go out of bounds (not smaller than 0 and not greater than the array's length)
    private static boolean check(String letter, int i, int j) {
        System.out.println("checking " + i + " " + j);
        if (i >= 0 && i < diceLayout.length && j >= 0 && j < diceLayout[i].length) {
            return diceLayout[i][j].equals(letter);
        }
        return false;
    }
}
公共类骰子{
私有静态字符串[][]diceLayout=新字符串[4][4];//使用静态字符串数组代替按钮数组
公共静态void main(字符串[]args){
diceLayout[0]=新字符串[]{“a”、“b”、“c”、“d”};
diceLayout[1]=新字符串[]{“e”、“f”、“g”、“h”};
diceLayout[2]=新字符串[]{“i”、“j”、“k”、“l”};
diceLayout[3]=新字符串[]{“m”、“n”、“o”、“p”};
String word=“bfkl”;//简单测试
System.out.println(checkWordConnected(word));
}
专用静态布尔校验字连接(字符串字){
word=word.toLowerCase();
String[]字母=word.split(“”);
int[]nextPosition=getFirstPosition(字母[0]);//nextPosition将始终保存您要查找的字母的i和j索引
如果(nextPosition!=null){//如果它不是null,那么单词的第一个字母就在数组中
系统