簡體   English   中英

比較方法和數組

[英]comparing methods and arrays

大家好,我一直在編寫代碼,幾乎可以正常工作,但是在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);
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM