簡體   English   中英

驗證用戶字符串輸入

[英]Validate user string input

編寫一個簡單的 HiLo 紙牌游戲,其中用戶從一副紙牌中獲得一個紙牌值,然后輸入“更高”、“更低”或“相等”,試圖猜測下一張紙牌的價值。

只是真的無法通過迭代來解決用戶輸入驗證問題,即。 在輸入具有所需參數的字符串之前不會繼續。

到目前為止我的代碼:

import java.util.Scanner;
import java.util.Random;

public class HiLoGame {

public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    Random randomCard = new Random();
    int numberOfSuccesses = 0;
    boolean finished = false;
    int card = (randomCard.nextInt(13) + 2);
    while (finished != true) {
        int nextCard = (randomCard.nextInt(13) + 2);
        String pictureCard = "";
        if (((numberOfSuccesses < 0) ? nextCard : card) == 11) {
            pictureCard = "Jack";
        } else if (((numberOfSuccesses < 0) ? nextCard : card) == 12) {
            pictureCard = "Queen";
        } else if (((numberOfSuccesses < 0) ? nextCard : card) == 13) {
            pictureCard = "King";
        } else if (((numberOfSuccesses < 0) ? nextCard : card) == 14) {
            pictureCard = "Ace";
        }

        System.out.println("The card is a " + ((card > 10) ? pictureCard : card));

        if (numberOfSuccesses == 4) {
            System.out.println("Congratulations. You got them all correct");
            finished = true;
            break;
        }
        while (!reader.nextLine().toLowerCase().equals("higher")
                || !reader.nextLine().toLowerCase().equals("lower")
                || !reader.nextLine().toLowerCase().equals("equal")) {
            System.out.println("Try again!");
            reader.next();
        }

        String userGuess = reader.nextLine().toLowerCase();

//TODO validate input
        if (userGuess.equals("higher")) {
            if (nextCard > card) {
                numberOfSuccesses++;
            } else {
                finished = true;
                break;
            }
        } else if (userGuess.equals("lower")) {
            if (nextCard < card) {
                numberOfSuccesses++;
            } else {
                finished = true;
                break;
            }
        } else if (userGuess.equals("equal")) {
            if (nextCard == card) {
                numberOfSuccesses++;
            } else {
                finished = true;
                break;
            }
        }

        System.out.println(numberOfSuccesses);
        card = nextCard;
    }
    if (numberOfSuccesses < 4) {
        System.out.println("Sorry, incorrect!");
    }
}
}

以及相關的代碼摘錄:

 while (!reader.nextLine().toLowerCase().equals("higher")
            || !reader.nextLine().toLowerCase().equals("lower")
            || !reader.nextLine().toLowerCase().equals("equal")) {
        System.out.println("Try again!");
        reader.next();
    }

它有點卡在上面的部分,反復“再試一次”。 我已經完成了必須使用 .hasNextInt() 的程序,但我正在為這個字符串驗證而苦苦掙扎。

感謝您的任何和所有幫助/評論!

您最多調用reader.nextLine() 3 次,因此您正在比較 3 個不同的字符串。

如果我輸入“xxx”,您的代碼會顯示“xxx !=更高,所以請閱讀另一行”-它永遠不會將“xxx”與“更低”或“相等”進行比較。

還要注意&& vs || .

解決方案是將一行讀入一個變量,並為每個條件使用該變量。 我不打算寫出來,因為這顯然是家庭作業或自學練習,所以最好你自己做。

我認為你的條件邏輯需要改變。 您正在檢查輸入是否不等於“更高”或不等於“更低”或不等於“等於”,因此即使您輸入預期值,它也始終是錯誤的 - 如果您輸入“更高”,它不等於更低. 您需要將 ors 更改為 ands。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM