我的Java代码输入验证有什么问题?

时间:2015-11-30 18:33:02

标签: java arrays string validation input

因此,该程序应该是用户输入短语而另一个用户试图猜测该短语的游戏。但是,我在购买元音时遇到了问题。无论我在第41行输入什么(它只是越过循环。我制作了validAnswer(字符串答案)方法,希望它在循环中有用,但它看起来并没有多大帮助。我知道我遗失了一件小事,但我还不太认识Java。我做错了什么?

我为很多代码道歉。

import java.util.Scanner;

public class PhraseGame {

public static void main(String[] args) {

    // Initialize scanner
    Scanner stdIn = new Scanner(System.in);

    // Initialize variables
    String sPhrase;
    String answer = "f";
    char cGuess = 0;
    char vGuess = 0;
    int count = 0;
    int vowels = 0;
    int consonants = 0;
    int spaces = 0;
    boolean gameOver = false;
    boolean correct = true;

    // Start the "fun" game
    System.out.print("Please enter the phrase to guess at: ");
    sPhrase = stdIn.nextLine();

    // Create the temporary Array
    char [] tmpArr = new char[sPhrase.length()];
    for (int i = 0; i < sPhrase.length(); i++) {
        tmpArr[i] = sPhrase.charAt(i);
    }

    // Gets the number of spaces
    spaces = initTemplateArray(sPhrase, tmpArr, spaces);
    printTemplateArray(tmpArr);

    while (!(endGame(gameOver, spaces, consonants, vowels, sPhrase))) {
        cGuess = getConsonant(stdIn, cGuess);

        do {
            System.out.print("\nWould you like to buy a vowel?: ");
            answer = stdIn.next();

            if (answer == "y") {
                getVowel(stdIn, vGuess); }

            else if (answer == "n"){
                break;
            }
        } while (!validAnswer(answer));


        // Updates the array and prints it
        updateTemplateArray(tmpArr, sPhrase, cGuess, vGuess, count, vowels, consonants);
        printTemplateArray(tmpArr);

        // Checks if the game is over
        endGame(gameOver, spaces, consonants, vowels, sPhrase);
    }

}


// returns the number of space characters used in the common phrase
public static int initTemplateArray(String sPhrase, char [] tmpArr, int spaces) {
    for (int i = 0; i < sPhrase.length(); i++) {
        if (tmpArr[i] != ' ') {
            tmpArr[i] = '?';
        } else {
            tmpArr[i] = ' ';
            spaces++;
        }
    }

    return spaces;
}



public static void printTemplateArray(char [] tmpArr) {
    System.out.println("\nCommon Phrase");
    System.out.println("-------------");
    for (int i = 0; i < tmpArr.length; i++) {
        System.out.print(tmpArr[i]);
    }
}

public static boolean isVowel(char c) {
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

public static char getConsonant(Scanner stdIn, char cGuess) {
    do {
        System.out.println("\nEnter a lowercase consonant guess: ");
        cGuess = stdIn.next().charAt(0);
    } while (isVowel(cGuess));

    return cGuess;
}

public static char getVowel(Scanner stdIn, char vGuess) {
    do {
        System.out.println("\nEnter a lowercase vowel guess: ");
        vGuess = stdIn.next().charAt(0);
    } while (!(isVowel(vGuess)));

    return vGuess;
}

public static int updateTemplateArray(char [] tmpArr, String sPhrase, char cGuess, char vGuess, int count, int vowels, int consonants) {
    for (int i = 0; i < sPhrase.length(); i++) {
        if (cGuess == sPhrase.charAt(i)) {
            tmpArr[i] = sPhrase.charAt(i);
            count++;
            consonants++;
        }

        if (vGuess == sPhrase.charAt(i)) {
            tmpArr[i] = sPhrase.charAt(i);
            count++;
            vowels++;
        }
    }

    return count & vowels & consonants;
}

public static boolean endGame(boolean gameOver, int spaces, int consonants, int vowels, String sPhrase) {
    int total = spaces + consonants + vowels;

    if (total == sPhrase.length()) {
        return true;
    }

    else {
        return false;
    }
}

public static boolean validAnswer(String answer) {
    if (answer.equalsIgnoreCase("y")) {
        return true;
    }

    else if (answer.equalsIgnoreCase("n")) {
        return true;
    }

    else {
        return false;
    }

}

}

1 个答案:

答案 0 :(得分:0)

您需要检查是否为空。

case-switch

或其他非常规方式。

public static boolean validAnswer(String answer) {
    if (answer!=null && (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n"))) {
        return true;
    }
    return false;
}

您需要修复

public static boolean validAnswer(String answer) {
    if ("y".equalsIgnoreCase(answer) || "n".equalsIgnoreCase(answer))) {
        return true;
    }
    return false;
}