java - 仅在输入为单个字母时运行程序

标签 java

我一直在为我的类(class)开发一个 Java 项目,我快完成了。该程序是一个刽子手游戏,用户输入一个字母,程序继续运行取决于该字母是否在单词中。我遇到的问题是我无法弄清楚如何做到这一点,所以当用户输入多个字母、数字或符号时,程序会打印出一条声明“输入无效,重试”并让用户再次输入一些东西,而不是显示它是一个错过的尝试或“字母”不在单词中。这是我的代码:

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class Hangman {

    private Scanner in = new Scanner(System.in);
    private boardPiece[] board = {new boardPiece(0),new boardPiece(0),new boardPiece(3),
            new boardPiece(1),new boardPiece(2),new boardPiece(0)};
    private String puzzle; 
    private String puzzleWord;
    private int puzzleIndex;
    private Random generator = new Random(System.currentTimeMillis());
    private boolean win;
    private boolean over;
    private String correct = "";
    private char guesses[] = new char[26];
    private int guessed;
    private int misses;
    private String puzzles[] = new String[5];

    public static void main(String [] args){

        String letter;
        String again = "y";

        Hangman game = new Hangman();

        try{
            BufferedReader in = 
                new BufferedReader(new FileReader("puzzles.txt"));

            int count = 0;
            while (in.ready()) { //while there is another line in the input file
                game.puzzles[count] = in.readLine(); //get line of data
                count++; //Increment CWID counter
            }
            in.close(); 
        }
        catch(IOException e){
            System.out.println("Error during reading/writing");
        }

        System.out.println("Welcome to HangMan!");
        System.out.println("To play, guess a letter to try to guess the word.");
        System.out.println("Every time you choose an incorrect letter another");
        System.out.println("body part appears on the gallows. If you guess the");
        System.out.println("word before you're hung, you win");
        System.out.println("If you get hung, you lose");
        System.out.println();
        System.out.println("Time to guess...");

        while(again.charAt(0) == 'y'){
            game.displayGallows();

            while(!game.over){
                game.printBoard();
                game.printLettersGuessed();
                System.out.println("The word so far: " + game.puzzle);
                System.out.println("Choose a letter: ");
                letter = game.in.next();

                game.guesses[game.guessed] = letter.charAt(0);
                game.guessed++;
                game.sort();

                if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
                    game.correct = game.correct + letter.charAt(0);
                    game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
                    if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
                        game.win = true;
                        game.over = true;
                    }

                }
                else
                    game.PrintWrongGuesses();
            }
            game.printBoard();
            System.out.println("Solution: " + game.puzzleWord);
            if(game.win)
                System.out.println("Congratulations! You've solved the puzzle!");
            else
                System.out.println("You failed, failer!");

            System.out.println();
            System.out.println("Congratulations, you win!");
            System.out.println("Do you want to play again? (y/n)");
            again = game.in.next();
        }
        System.out.println("Goodbye!");
    }

    public void displayGallows(){

        win = false;
        over = false;

        board[0].piece = "    ______ ";
        board[1].piece = "    |    | ";
        board[2].piece = "         | ";
        board[3].piece = "         | ";
        board[4].piece = "         | ";
        board[5].piece = "  _______| ";

        puzzleIndex = generator.nextInt(puzzles.length);
        puzzleWord = puzzles[puzzleIndex];

        puzzle = puzzleWord.replaceAll("[A-Za-z]","-");
        correct = "";
        for(int x=0;x<26;x++)
            guesses[x] = '~';
        guessed = 0;
        misses = 0;
    }

    public void printBoard(){
        for(int x =0;x<6;x++)
            System.out.println(board[x].piece);
    }

    public void PrintWrongGuesses(){
        misses++;
        System.out.println();
        if(misses == 1){
            board[2].piece = "    0    | ";
            System.out.println("Number of misses: " + misses);
        }
        else if(misses == 2){
            board[2].piece = "   \\0    | ";
            System.out.println("Number of misses: " + misses);
        }
        else if(misses == 3){
            board[2].piece = "   \\0/   | ";
            System.out.println("Number of misses: " + misses);
        }
        else if(misses == 4){
            board[3].piece = "    |    | ";
            System.out.println("Number of misses: " + misses);
        }
        else if(misses == 5){
            board[4].piece = "   /     | ";
            System.out.println("Number of misses: " + misses);
        }
        else if(misses == 6){
            board[4].piece = "   / \\   | ";
            System.out.println("Number of misses: " + misses);
            over = true;
        }

    }

    public void printLettersGuessed(){
        System.out.print("Letters guessed already: ");
        for(int x=0;x<26;x++){
            if(guesses[x] != '~')
                System.out.print(guesses[x] + " ");
        }
        System.out.println();
    }

    public void sort(){
        boolean doMore = true;
        while (doMore) {
            doMore = false;  // assume this is last pass over array
            for (int i=0; i<guesses.length-1; i++) {
                if (guesses[i] > guesses[i+1]) {
                    char temp = guesses[i];  
                    guesses[i] = guesses[i+1];  
                    guesses[i+1] = temp;
                    doMore = true;  // after an exchange, must look again 
                }
            }
        }
    }

    class boardPiece{
        public String piece;
        public int total;
        public int used;

        boardPiece(int x){
            used = 0;
            total = x;
        }
    }   
}

最佳答案

        System.out.println("Choose a letter: ");
        letter = game.in.next();

        if(letter.length() == 1)
        {
            game.guesses[game.guessed] = letter.charAt(0);
            game.guessed++;
            game.sort();

            if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
                game.correct = game.correct + letter.charAt(0);
                game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
                if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
                    game.win = true;
                    game.over = true;
                }

            }
            else
                game.PrintWrongGuesses();
        }else
        {
            System.out.println("Invalid input, try again");
        }

关于java - 仅在输入为单个字母时运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400091/

相关文章:

java - Android 的 Java 中一个非常奇怪的 for 循环错误?

java - 包装器找不到 servlet 类 Eclipse 停止为 Tomcat 生成类文件

java - Android Studio Java JDK环境路径

Java Date/Calendar 对象字符串,比较

Java二分查找计数比较次数

java - 参数 [variable] 的非法修饰符;只允许 final

java - 在java中开发游戏时线程 "main"java.lang.NullPointerException中的异常

java - 从 Android 模拟器调用 REST Web 服务

java - 如何在Android中正确获取文件的URI?

java - 我想知道为什么这个小程序需要 O(N) 才能继续