简体   繁体   English

为什么我的 java while 循环继续以真实状态打印?

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

Below I have an array and I am checking with the user input guessing the card type in my wallet?下面我有一个数组,我正在检查用户输入,猜测我钱包中的卡类型? if true, exit the while loop else ask the user again until matches the same card type in my array.如果为真,则退出 while 循环,否则再次询问用户,直到匹配我的数组中的相同卡片类型。 My issue is when the user guesses correctly, I still getting "card type incorrect try again message".我的问题是当用户正确猜测时,我仍然收到“卡类型不正确再试一次消息”。 is there a way to shorten my code or make it better to correct it?有没有办法缩短我的代码或更好地纠正它?

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;
}

Because the loop is not terminated, it continues until the end.因为循环没有终止,它一直持续到结束。 You can return the value from the for loop in case it's a match如果匹配,您可以从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");
    }
}

Because you're not putting those System.out lines in an appropiate if clause.因为您没有将这些 System.out 行放在适当的 if 子句中。

Note that you're running a for loop without actually being sure of the number of iterations that loop needs to run - as the definition is "keep going through all of them unless one of them is correct. If one's correct, stop".请注意,您正在运行 for 循环,但实际上并不确定循环需要运行的迭代次数 - 因为定义是“继续遍历所有循环,除非其中一个正确。如果正确,则停止”。

This would be the resultant code of making those two fixes.这将是进行这两个修复的结果代码。

Also bear in mind that using a break operand is not recommended because it goes against the structured programming paradigm reasoning - it "breaks" the structure.还要记住,不建议使用中断操作数,因为它违背了结构化编程范式推理——它“破坏”了结构。 Therefore, even if it can be used to improve your code, I advise you to follow this proposal instead.因此,即使它可以用于改进您的代码,我建议您改为遵循此建议。

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;
    }

Try This尝试这个

    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