简体   繁体   中英

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. 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

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.

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM