繁体   English   中英

Java将单个字符与字符数组进行比较

[英]Java Compare single char to char array

我需要将单个char与char数组进行比较,看看array是否具有该char。

我当前的代码如下所示:

public boolean isThereChar(char[] chaArray, String chr){
    boolean bool = false;
    for(int i=0; i < chaArray.length; i++)
            {
                if(chr.equals(chaArray[i])){
                    bool = true;
                }
            }
            return bool;
}

编辑注释:

真抱歉,造成混淆! 我只是Java初学者= /
基本上我是用GUI编写小型Hangman游戏。
我的程序读取了文本文件,并随机选择了玩家必须猜测的单词,然后以隐藏方式将其打印出来,如下所示:_ _ _ _ _
在这种情况下,我希望玩家输入字符或字符串(人们可以猜测整个单词或仅一个字母)
然后,我希望我的程序采用该字母或字符串并与隐藏的单词进行比较

以下代码选择单词并将其隐藏:

public String pickWord(){
    String guessWord = (wordsList[new Random().nextInt(wordsList.length)]);
    return guessWord.toLowerCase();
}

//Hides picked word
public char[] setWord(){
    char[] word = new char[pickWord().length() * 2];
    for (int i = 0; i < word.length; i+=2) {
        word[i] = '_';
        word[i + 1] = ' ';
    }
    return word;
}

然后人输入他想用以下代码编程的字符:

public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();

    if (action == "Guess Letter"){
        inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inputChar.length() > 1){
            GuessedLetters glr = new GuessedLetters(inputChar);
            glr.setInString(inputChar);
            //For testing purposes
            System.out.println("This is String: " +glr.getInString());              
        }else{
        GuessedLetters glr = new GuessedLetters(inputChar);
        glr.setInChar(inputChar);
        //For testing purposes
        System.out.println("This is Char: " +glr.getInChar());
        }
    }

最后,我要输入的字符与我隐藏的单词字符数组进行比较:

public boolean isThereChar(char[] array, String str){
    return isThereChar(array, str.charAt(0));
}

public boolean isThereChar(char[] array, char c){
    for(int i=0; i<array.length; i++){
        if (array[i] == c) return true;
    }
    return false;
}

我想检查我的代码返回什么(对或错),但是我一直失败。 (现在,我正在尝试在主类中调用method进行检查,如果可以给我提示如何做的话,否则请告诉我。)

我将使用: Chars.contains(array, chr); 番石榴糖

之所以发生NullPointerException,是因为调用该方法时chaArraychrnull 如果不是,则NullPointerException发生在其他地方!

您的代码的另一个问题是这一行:

  if (chr.equals(chaArray[i])) {

由于chr实际上是一个String,因此这里将发生的是chaArray[i]的值将自动装箱为Character对象,然后作为参数传递给String.equals(Object) 但是,除非String.equals(Object)的参数为String否则它将返回false ,因此您的代码将始终找不到该字符。

您需要像这样比较字符:

  if (chr.charAt(0) == chaArray[i]) {

或将chr声明为char并将其比较为:

  if (chr == chaArray[i]) {

让我们看看我是否满足您的需求:

public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    if (action == "Guess Letter"){
        inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inputChar.length() > 1){ //User input is a string here, right?
            GuessedLetters glr = new GuessedLetters(inputChar);
            glr.setInString(inputChar);
            System.out.println(wordToGuess.contains(glr.getInString())); //This will print true if wordToGuess is equal to glr.getInString() or if it just contains it
            //For testing purposes
            System.out.println("This is String: " +glr.getInString());              
        }else{ //Here the user gave us just a character, so we've got to know if this character is contained in the word, right?
        GuessedLetters glr = new GuessedLetters(inputChar);
        glr.setInChar(inputChar);
        System.out.println(wordToGuess.contains(glr.getInChar()); //This will print true if your char is in the wordToGuess string
        //For testing purposes
        System.out.println("This is Char: " +glr.getInChar());
        }
    }
}

String chr可能为null,从而导致NullPointerException

使用char chr而不是String

public boolean isThereChar(char[] chaArray, char chr){
    boolean bool = false;
    for(int i=0; i < chaArray.length; i++) {
        if(chr==chaArray[i])){
             bool = true;
        }
    }
    return bool;
}

从传入的参数中选择字符,或传入一个字符,例如

chr[0]

要么

public String isThereChar(char[] chaArray, char chr){
    for(int i=0; i < chaArray.length; i++)
            {
                if(chr.equals(chaArray[i])){
                    return chr;
                }
            }
            return "Guess Again";
}
public boolean  isThereChar(char[] chaArray, char chr){        
for(int i=0; i < chaArray.length; i++)
        {
            if((chaArray[i]==chr)){
                return true;   // means Character exist in the Character array
            }
        }
        return false;  //// means Character does not exist in the Character array
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM