簡體   English   中英

為什么我的 java while 循環繼續以真實狀態打印?

[英]Why is my java while loop continue printing with true status?

下面我有一個數組,我正在檢查用戶輸入,猜測我錢包中的卡類型? 如果為真,則退出 while 循環,否則再次詢問用戶,直到匹配我的數組中的相同卡片類型。 我的問題是當用戶正確猜測時,我仍然收到“卡類型不正確再試一次消息”。 有沒有辦法縮短我的代碼或更好地糾正它?

public static void main(String[] args) {
    String correctGuess = validateGuessedCardType();
    System.out.println(correctGuess);
}

private static final String[] cardsTypesInWallet = {
    "DBS",
    "POSB",
    "AMEX",
    "Standard Charted"
};

private static String validateGuessedCardType() {
    boolean correctGuess = false;

    String guessedCard = "";
    while (!correctGuess) {
        in = new Scanner(System.in);
        System.out.println("Guess a card in my wallet");
        guessedCard = in.nextLine();

        for (int i = 0; i < cardsTypesInWallet.length; i++) {
            if (cardsTypesInWallet[i].equals(guessedCard)) {
                correctGuess = true;
            }
        }
        System.out.println("Card Type is incorrect");
        System.out.println("try again");
    }
    return guessedCard;
}

因為循環沒有終止,它一直持續到結束。 如果匹配,您可以從for循環返回值

private static String validateGuessedCardType() {

    String guessedCard = "";
    while (true) {
        in = new Scanner(System.in);
        System.out.println("Guess a card in my wallet");
        guessedCard = in.nextLine();

        for (int i = 0; i < cardsTypesInWallet.length; i++) {
            if (cardsTypesInWallet[i].equals(guessedCard)) {
                return guessedCard;
            }
        }

        System.out.println("Card Type is incorrect");
        System.out.println("try again");
    }
}

因為您沒有將這些 System.out 行放在適當的 if 子句中。

請注意,您正在運行 for 循環,但實際上並不確定循環需要運行的迭代次數 - 因為定義是“繼續遍歷所有循環,除非其中一個正確。如果正確,則停止”。

這將是進行這兩個修復的結果代碼。

還要記住,不建議使用中斷操作數,因為它違背了結構化編程范式推理——它“破壞”了結構。 因此,即使它可以用於改進您的代碼,我建議您改為遵循此建議。

private static String validateGuessedCardType() {
        boolean correctGuess = false;

        String guessedCard = "";
        while (!correctGuess) {
            in = new Scanner(System.in);
            System.out.println("Guess a card in my wallet");
            guessedCard = in.nextLine();

            int i=0;
            while(i < cardsTypesInWallet.length && !correctGuess) {
                if (cardsTypesInWallet[i].equals(guessedCard)) {
                    correctGuess = true;
                }
                else i++;
            }
            if(!correctGuess){
                System.out.println("Card Type is incorrect"); System.out.println("try again");
            }
        }
        return guessedCard;
    }

嘗試這個

    public class Test1 {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        String correctGuess = validateGuessedCardType();
        System.out.println(correctGuess);
    }

    private static final String[] cardsTypesInWallet = {
            "DBS",
            "POSB",
            "AMEX",
            "Standard Charted"
    };

    private static String validateGuessedCardType() {
        boolean correctGuess = false;
        String guessedCard = "";
        while (!correctGuess) {

            System.out.println("Guess a card in my wallet");
            guessedCard = in.nextLine();

            for (int i = 0; i < cardsTypesInWallet.length; i++) {
                if (cardsTypesInWallet[i].equals(guessedCard)) {
                    correctGuess = true;
                }
            }
            if(correctGuess) {
                System.out.println("Card Type is correct");
            } else {
                System.out.println("Card Type is incorrect");
                System.out.println("try again");
            }
        }
        return guessedCard;
    }
}
    private static String validateGuessedCardType() {
        boolean correctGuess = false;

        String guessedCard = "";
        outer:
        while (!correctGuess) {
            in = new Scanner(System.in);
            System.out.println("Guess a card in my wallet");
            guessedCard = in.nextLine();

            for (int i = 0; i < cardsTypesInWallet.length; i++) {
                if (cardsTypesInWallet[i].equals(guessedCard)) {
                    break outer;
                }
            }
            System.out.println("Card Type is incorrect");
            System.out.println("try again");
        }
        return guessedCard;
    }


OR

    private static final List<String> cardsTypesInWallet = Arrays.asList{
      "DBS",
      "POSB",
      "AMEX",
      "Standard Charted"
  };

    private static String validateGuessedCardType() {
      boolean correctGuess = false;

      String guessedCard = "";
      while (!correctGuess) {
          in = new Scanner(System.in);
          System.out.println("Guess a card in my wallet");
          guessedCard = in.nextLine();

          if (cardsTypesInWallet.contains(guessedCard){
  correctGuess = true;
 }
    else {
   System.out.println("Card Type is incorrect");
          System.out.println("try again");
}

      }
      return guessedCard;
  }

暫無
暫無

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

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