比较方法和数组

时间:2014-12-12 15:54:18

标签: java arrays while-loop

嘿伙计们我一直在研究我的代码,几乎让它正常工作,但是在方法numGuess和hideWord之间的某处我有一个逻辑错误,不是比较两个 我想要的方式。我正在尝试更换星号。因此,如果用户输入是我想要的“a” 星号填充“a”进行更新。

public class Lab12 {

    public static final int MAXWORD = 15000;
    public static final int MAXCHAR = 20;
    public static final int MAXGUESS = 8;

    public static void main(String[] args) {//main
        Scanner keyboard = new Scanner(System.in);
        int cout = 0;
        //method calling bulk of other methods
        storage();

    }//end main

    public static int pickrandom(int count)//generates random number
    {
        Random generator = new Random();
        return generator.nextInt(count);
    }

    public static int storage()//holds bulk of other methods also performs intro and reads in file
    {
        String wordList[] = new String[MAXWORD];
        String wordLetter[] = new String[MAXCHAR];
        String dictionary = "dictionary.txt";
        Scanner readFileIn = null;
        String dictionaryVal = " ";
        int count = 0;
        String cont = "";

        try//Try to read in the file.
        {
            readFileIn = new Scanner(new File(dictionary));//creates object scanner and object file?

            while (readFileIn.hasNextLine()) {
                wordList[count] = readFileIn.nextLine();
                count++;
            }

            System.out.println("");
            System.out.println("H A N G M A N");
            System.out.println("");
            System.out.println("This is a word guessing game A word will be selected at random");
            System.out.println("and kept hidden. You will try to figure out the secret word by");
            System.out.println("guessing letters which you think are in the word. You will guess");
            System.out.println("one letter at a time. If the letter you guess is correct, the");
            System.out.println("position(s) of the letter in the secret word will be shown.");
            System.out.println("You will be allowed 8 wrong guesses. If you guess incorrectly 8");
            System.out.println("times, you lose the game. If you guess all of the letters in the");
            System.out.println("word, you win.");
            System.out.println("");

            int rand = pickrandom(count);
            String hiddenWord = wordList[rand];
            char[] asterisks = new char[MAXCHAR];

            clearScreen(cont);//clear screen method call
            hideWord(hiddenWord);//hidden word method call
            System.out.println((hideWord(hiddenWord)));//print out hidden word in asterisks
            System.out.println("");
            System.out.println("");
            numGuess(hiddenWord, asterisks);//method call to number of guesses/comparisions

        } catch (Exception e)//Catch error when trying to open/find the file dictionary.txt
        {

            System.out.println("Error can not open dictionary.txt");
        }
        return count;
    }

    public static void clearScreen(String cont)//clear screen method for user
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Press enter to continue:");
        cont = keyboard.nextLine();
        if (cont.equals(""));
        {
            for (int i = 0; i < 100; i++) {
                System.out.println("");
            }
        }
    }

    public static String hideWord(String hiddenWord)//puts hidden word in asterisks
    {
        int wordLength = hiddenWord.length();
        char[] asterisks = new char[wordLength];

        for (int i = 0; i < wordLength; i++) {
            asterisks[i] = '*';//couldn't figure out how to double space asterisks
        }
        String hideAsterisks = String.valueOf(asterisks);
        return hideAsterisks;//return the value of 
    }

    public static void numGuess(String hiddenWord, char[] asterisks)//compares guesses and counts
    //attempts, however I feel like
    //like I'm so close but I have a
    //logic error in comparisons.
    {
        Scanner keyboard = new Scanner(System.in);
        String hiddenword = hideWord(hiddenWord);
        int remAttempts = MAXGUESS;
        char attempts;
        do {
            System.out.println("Enter a letter or 9 to quit");
            attempts = keyboard.next().charAt(0);
            remAttempts--;
            System.out.println("Attempts remaining: " + remAttempts);
            for (int i = 0; i < hiddenWord.length() - 1; i++) {
                if (attempts == (hiddenword.charAt(asterisks[i])))//trying to compare user input to *
                {
                    System.out.println("Nice job!");

                }
            }
        } while (attempts != '9' && remAttempts > 0);
    }
}

1 个答案:

答案 0 :(得分:1)

在Java中进行字符串操作的方法有很多种。最快捷的方法之一可能是使用REGEX。但我告诉你使用循环并检查每个字符。

String answer = "hello";
String hidden = answer.replaceAll(".", "*");  //let hidden fill with *

char guess = 'e';  //Get this from scanner

for(int x=0; x<answer.length(); x++)
    if(answer.charAt(x) == guess)
        hidden = hidden.substring(0,x) + guess + hidden.substring(x+1);