【问题标题】:comparing methods and arrays比较方法和数组
【发布时间】:2014-12-12 15:54:18
【问题描述】:

嘿,伙计们,我一直在编写我的代码并且几乎可以正常工作,但是在方法 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);
    }
}

【问题讨论】:

  • 为什么不直接问你想知道什么?写了这么长的问题,我真的不知道你想要什么。
  • 如何将星号的值发送到我的 numGuess 方法,然后要求用户输入一个字符,如果他/她的字符与星号的值匹配,用他/她的字符替换它
  • Char array updating 的可能重复项
  • 在您的clearScreen 方法中,您有一个不起作用的ifif (cont.equals("")); {...}; 就像一个空语句,告诉 java if 已经完成,所以大括号 {...} 中的代码将始终执行——它独立于 if
  • 顺便说一句:你可以写\n换一个新行——这样你就不必连续使用这么多System.out.println:)

标签: java arrays while-loop


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2014-07-06
    • 2019-07-29
    • 2019-07-12
    • 2014-05-15
    相关资源
    最近更新 更多