简体   繁体   中英

comparing methods and arrays

Hey guys I have been working on my code and almost got it working correctly, however somewhere between the methods numGuess and hideWord I have a logic error that is not comparing the two the way I would like. I'm trying to replace the asterisks. So of if the user input is "a" I want asterisks filled with "a" to update.

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

There are many ways to do String manipulation in Java. One of the quickest ways might be using REGEX. But I am showing you using loop and checking each characters.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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