Java将用户输入与char数组进行比较

时间:2016-01-06 02:33:18

标签: java arrays string char

首先你好,新来的。我对Java很新,我在使用char类将用户输入与动态创建的char数组进行比较时遇到了一些问题。我有一个10个单词的列表,程序随机选择一个,然后将其转换为char数组。我已经完成了所有工作。我在将用户输入与数组中的String进行比较时遇到问题。我基本上练习import java.util.Random; import java.util.Scanner; public class Hangman { public static void main(String[] args) { String[] words = new String[10]; words[0] = "Elsa"; words[1] = "Anna"; words[2] = "Olof"; words[3] = "Sphen"; words[4] = "Christoph"; words[5] = "TinkerBell"; words[6] = "Arial"; words[7] = "SnowWhite"; words[8] = "Cinderella"; words[9] = "SleepingBeauty"; Random selWord = new Random(); int newWord = selWord.nextInt(10); char[] wordLetters = words[newWord].toCharArray(); Scanner userInput = new Scanner(System.in); System.out.println("Please make your first guess"); for (int i=0; i < words[newWord].length(); i++) { System.out.print("_ "); } if (!userInput.hasNextLine()) { do { System.out.println("Please enter letters only."); } while (!userInput.hasNext()); } for (int n=0; n < wordLetters.length; n++) { String userChoice = userInput.nextLine(); if (wordLetters[n] == userChoice.charAt(n)) { System.out.println("You've made a match"); } else { System.out.println("Sorry, try again."); } } userInput.close(); // System.out.println(words[newWord]); // System.out.println(words[newWord].length()); } } 操纵,并创造了一个我女儿可以愚弄的简单挂人游戏。任何建议将不胜感激,一些信息的有用链接也将是伟大的。提前谢谢。

{{1}}

3 个答案:

答案 0 :(得分:1)

问题是您希望用户在每一行输入一个字符,然后将其与userChoice.charAt(n)进行比较。您应该将其与userChoice.charAt(0)处的字符进行比较。

改变这个:

if (wordLetters[n] == userChoice.charAt(n))

要:

if (wordLetters[n] == userChoice.charAt(0))

除此之外,我认为下面的检查应该在for之前进行String userChoice = userInput.nextLine()循环:

if (!userInput.hasNextLine()) {
        do {
            System.out.println("Please enter letters only.");
        } while (!userInput.hasNext());
    }

所以你的for循环应该是这样的:

for (int n=0; n < wordLetters.length; n++) {
        while (!userInput.hasNextLine()) {
            System.out.println("Please enter letters only.");
        }
        String userChoice = userInput.nextLine();

        if (wordLetters[n] == userChoice.charAt(0)) {
            System.out.println("You've made a match");
        } else {
            System.out.println("Sorry, try again.");
        }
    }

答案 1 :(得分:0)

我在代码上方运行了你的工作。 我已经放了一个System.out.println(newWord);要知道随机拾取的是什么号码,如果我输入相同的数字就会从数组中说出“你已经匹配了”

enter image description here

答案 2 :(得分:0)

我读了你的代码,似乎完全无法播放。

所以我在这里做了一些改变

      String[] words = new String[10];

        words[0] = "Elsa";
        words[1] = "Anna";
        words[2] = "Olof";
        words[3] = "Sphen";
        words[4] = "Christoph";
        words[5] = "TinkerBell";
        words[6] = "Arial";
        words[7] = "SnowWhite";
        words[8] = "Cinderella";
        words[9] = "SleepingBeauty";

    Random selWord = new Random();
    int newWord = selWord.nextInt(10);

    char[] wordLetters  = words[newWord].toCharArray();
    boolean[] CheckBox  = new boolean[wordLetters.length];//check each letter is correct
    boolean checkRoundbyRound = false;//check the round have make matchs
    int correctWords=0;//count how many letter is matched
    for(int n=0;n< wordLetters.length; n++) CheckBox[n]=false;//initail
    Scanner userInput;

    System.out.println("Let's start");

    while(true)
    {               
        //initail 
        checkRoundbyRound=false;
        correctWords=0;

        //Hanging
        for (int n=0; n < words[newWord].length(); n++) 
        {
            if(CheckBox[n]) 
                System.out.print(wordLetters[n]+" ");
            else
                System.out.print("_ ");
        }

        System.out.println("\nPlease enter letters only.");
        userInput = new Scanner(System.in);
        String userChoice = userInput.nextLine();

        for (int n=0; n < wordLetters.length; n++) 
        {
            for(int m=0;m<userChoice.length();m++)
            {   
                //remember change the case, just for case :D
                if (Character.toLowerCase(wordLetters[n]) == Character.toLowerCase(userChoice.charAt(m))) 
                {
                    CheckBox[n] = true;
                    checkRoundbyRound = true;
                } 
            }
        }

        //Check have make match this round
        if(checkRoundbyRound)
            System.out.println("You've made a match");
        else
            System.out.println("Sorry, try again.");

        //count how many letters found
        for(int n=0;n<wordLetters.length;n++)
        {
            if(CheckBox[n])correctWords++;
        }

        //if all letters are correct, end game
        if(correctWords==wordLetters.length)
            break;
    }
希望你的女儿玩得开心。