资源下载比较单个字符到字符数组 [英] Java Compare single char to char array

查看:104
本文介绍了资源下载比较单个字符到字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较单个字符到字符数组,看看是否数组有字符。

I need to compare single char to char array and see if array has that char.

我目前的code是这样的:

My current code looks like this:

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。结果
我的程序读取过的文本文件,并随机选择其中玩家猜测的话,那么像这样打印出来的隐藏方式:_ _ _ _ _结果
在这种情况下,我想玩家输入的字符或字符串(人能猜到或者整个单词或只是一个字母)结果
然后我想我的程序采取信函或串并比较隐蔽我的话结果

Really sorry for being confusing! I am just a Java Beginner =/
Basically I am writing small Hangman game with GUI.
My program reads off text file and randomly chooses word which player has to guess, then prints it out in hidden manner like this: _ _ _ _ _
In this case I want player to input character or string (person can guess either whole word or just one letter)
Then I want my program to take that letter or string and compare to my hidden word

继$ C $,C选择单词,隐藏它:

Following code chooses word and hides it:

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

然后输入的人他的性格,他猜测有以下code编程:

Then person input his character which he guesses to program with following code:

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

最后,我想利用这是输入该字符,并比较我的char型数组这是我隐藏的话:

Lastly I want to take that character which was inputted and compare to my array of chars which is my hidden word:

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

我要检查什么呢我的code返回(true或false),但我一直在这样做失败。
(现在我试图调用方法在我的主类,以检查它,如果你可以给我提示,如何做到这一点,否则请让我知道。)

I want to check what does my code returns (true or false), but I keep failing at doing so. (Right now I am trying to call method in my main class to check it, if you can give me tips how to do it otherwise please let me know.)

推荐答案

让我们来看看,如果我得到你所需要的:

Let's see if I got what you need :

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

这篇关于资源下载比较单个字符到字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆