我似乎有一个无法解决的问题,当用户为我的程序输入完整的单词时,它会在每次搜索字符时显示,而不是仅仅显示整个单词,表明他们猜对了。当用户输入整个单词时,我怎么会只显示单词而不在每次搜索字符时都不显示呢?感谢您日后的回覆

package assignment1Q2;

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;
public class HangmanClassExample {

static Scanner keyboard = new Scanner(System.in);
static int play, size, size2;
static String word;
static String[] ARRAY = new String[0];


public static void main(String[] args) {

    setUpGame();
}

public static void setUpGame() {
    System.err.printf("Welcome to hangman.\n");

    try {

        Scanner scFile = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
        String line;
        while (scFile.hasNext()) {
            line = scFile.nextLine();
            Scanner scLine = new Scanner(line);
            size++;
        }
        ARRAY = new String[size];
        Scanner scFile1 = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
        while (scFile1.hasNext()) {
            String word;
            line = scFile1.nextLine();
            Scanner scLine = new Scanner(line);
            word = scLine.next();
            ARRAY[size2] = word;
            size2++;
            calculateGuess();
        }
    } catch (FileNotFoundException e) {
        System.out.println(e);
    }
}

public static void calculateGuess() {

    try {
        do {

            int random = (int) (Math.random() * ARRAY.length);
            String randomWord = ARRAY[random];
            String word = randomWord;
            char[] ranWord = randomWord.toCharArray();
            char[] dash = word.toCharArray();

            int LEFT = 6;
            for (int i = 0; i < dash.length; i++) {
                dash[i] = '-';
                System.out.print(dash[i]);
            }
      for (int A = 1; A <= dash.length;) {
            System.out.print("\nGuess a Letter:");
            String userletters = keyboard.next();;

            for (int i = 0; i < userletters.length(); i++) {
                char userLetter = userletters.charAt(i);
                String T = Character.toString(userLetter);
                for (int B = 0; B < ranWord.length; B++) {

                    if (userLetter == dash[B]) {
                        System.out.println("this '" + userLetter + "' letter already exist");
                        B++;
                        if (userLetter == dash[B-1]) {
                            break;
                        }


                    } else if (userLetter == ranWord[B]) {
                        dash[B] = userLetter;
                        A--;
                    }
                }
                if (!(new String(ranWord).contains(T))) {
                    LEFT--;
                    System.out.println("You did not guess a correct letter, you have " + LEFT + " OF "
                            + dash.length + " trys left to guess correctly");
                }

                System.out.println(dash);
            }
            if ((new String(word)).equals(new String(dash))) {
                System.out.println("\nYou have guessed the word correctly!");
                break;

            }

        }

            System.out.println("Play agian? (y/n)");
            String name = keyboard.next();

            if (name.equals("y")) {
                play = 0;

            } else {
                play = 1;
                return;
            }
        } while (play == 0);

    } catch (NullPointerException e) {

    }
}


}

输出:

Welcome to hangman.



--------

Guess a Letter:c

c-------

Guess a Letter:c

this 'c' letter already exist

c-------

Guess a Letter:computer

this 'c' letter already exist

c-------

co------

com-----

comp----

compu---

comput--

compute-

computer

You have guessed the word correctly!

Play agian? (y/n)

n

最佳答案

您可以使用startsWith来检查用户输入的字符,而不是使用两个for循环并检查每个字符。例如如果用户输入comp,则只需检查originalString.startsWith(comp)-如果为true,则仅打印comp并从originalString中删除前4个字符。

关于java - HangMan错误输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39771561/

10-12 20:52