新手遇到了一些严重的麻烦" Hangman" Java程序

时间:2016-09-20 17:41:28

标签: java

所以这是我的程序,但是当我运行它时,结果是不正确的。例如,如果计算机选择了这个'成为游戏的词,并且' t'输入时,标记为错误,而其他字母标记为正确。显然这里的事情是错的,但对于我的生活,我无法弄明白。任何帮助表示赞赏!

import java.util.Scanner;
import java.util.Random;
public class Hangman {

    public static void main(String[] args) {
        //set words to array
        String[] words = {"write", "this", "that", "blue", "school", "table"};

        //initialize vars
        int tryLeft = 6;                //user gets 6 wrong guesses
        int guessTot = 0;               //initializes user guess counter
        boolean fin = false;            //fin controls do-while loop; stands for "finished?"
        String guessChar;               //use if user enters only 1 letter
        String guessed = "";            //contains all previous guesses
        StringBuffer asterisk;          //StringBuffer allows strings to be mutable; ast = asterisks in the word to display

        //initialize scanner
        Scanner userInput = new Scanner(System.in);
        String word = randomWord(words);
        //while user has not correctly guessed word, and has not lost the game, do this:
        do {
            //call randomWord method

            //**System.out.println(word);;

            //call asterisk method, wordAsAst
            asterisk = wordAsAst(word);
            //**System.out.println(asterisk);

            System.out.println("Your word is: " + word);
            System.out.println("Enter a letter in the word: ");
            guessChar = userInput.next();

            if(guessChar.length() == 1) {           //checking that user entered only 1 character
                guessed = guessed + guessChar;
                if (word.indexOf(guessChar) == 0) {     //if guessChar is not present in the word...
                    tryLeft = tryLeft - 1;                      //remove one try
                    System.out.println("Incorrect guess");
                }

            else {                                  //if guessChar is present in teh word...
                checkWord(asterisk, word, guessChar);
                System.out.println("Good guess!");
            }
            if(tryLeft == 0) {
                System.out.println("You have run out of tries. Sorry!");
                fin = true;
            }
            if(word.equals(asterisk.toString())) {
                System.out.println("Congratulations, you've won!");
                fin = true;
            }

            }   
        }while(!fin);
    }
    public static String randomWord(String[] words) {
        int indWords = new Random().nextInt(words.length);
        String randWord = (words[indWords]);
        return randWord;

    }
    public static StringBuffer wordAsAst(String word) {
        StringBuffer asterisks = new StringBuffer(word.length());
        for (int i = 0; i<=word.length(); i++) {
            asterisks.append("*");}
        return asterisks;
        }

    public static StringBuffer checkWord(StringBuffer asterisk, String word, String guessChar) {
        for(int j = 0; j<word.length(); j++) {
            if(word.charAt(j) == guessChar.charAt(0)){
                asterisk.setCharAt(j, guessChar.charAt(0)); 
            }
        }
        return asterisk;
    }
    }

4 个答案:

答案 0 :(得分:1)

<android.support.design.widget.NavigationView android:id="@+id/nav_view1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true"> <ScrollView android:layout_width="wrap_content" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/nav_view" app:headerLayout="@layout/nav_header_admin" app:menu="@menu/activity_admin_drawer"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/lyNavFooter"> <!--INCLUDE YOUR FOOTER HERE --> </LinearLayout> </LinearLayout> </ScrollView> </android.support.design.widget.NavigationView> 行对我来说非常可疑。在Java中,String中元素的位置(也是数组和列表)从0到 Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null)); contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject)); startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser))); 。你的if语句询问word中guessChar的位置是否为0,即第一个位置

t h i s
0 1 2 3

t确实在单词&#34;中的位置0;&#34;。不要检查第一个位置,而是使用-1来检查guessChar是否在单词中。

您还可以使用String.contains(String)方法,它看起来像: if (word.indexOf(guessChar) == 0) {

答案 1 :(得分:1)

您的问题在第36行

if (word.indexOf(guessChar) == 0) {     //if guessChar is not present in the word...

documentation中描述的Java indexOf返回:

  

此对象表示的字符序列中第一次出现的字符的索引,如果未出现该字符,则为-1。

如果您开始猜测t的第一次猜测(单词为this),您的程序将输出0,因为t的单词中的索引为{ {1}}。

如果未出现该字符,this返回-1,则应将此行更改为:

indexOf

答案 2 :(得分:0)

更改此行:

word.indexOf(guessChar) == 0

到此:

word.indexOf(guessChar) == -1

答案 3 :(得分:0)

在以下行(n.36)中,检查字符串的第一个字符:

if (word.indexOf(guessChar) == 0) 

更改为

if (word.indexOf(guessChar) == -1)