简体   繁体   中英

How do I make my for loop run a set number of times before giving a certain output?

So my code should run as follows:

Ask the user their rating of Krabby Patties.

If the user inputs a number from 1-10 the code will thank them for their rating and end. If the user inputs a number outside of the range from 1-10, it will put them in a loop telling them what to input and repeating the question. It will end when they input a number from 1-10.

If the user inputs an incorrect value 3 times (the number of times I want to set my for loop to), it will tell the user to try again later!

Everything in my code works except it doesn't stop the loop after the user inputs 3 wrong values in a row.

Sorry if it isn't formatted perfectly and might be a stupid question, but I am new to coding.

Here is my code:

import javax.swing.JOptionPane;

public class LoopLab {
    public static void main(String[] args) {
        int MIN = 1;
        int MAX = 10;
        String userRating = JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". ");
        int userRating2 = Integer.parseInt(userRating);
        if (userRating2 >= MIN || userRating2 <= MAX) {
            for (int attempts = 1; userRating2 < MIN || userRating2 > MAX; userRating2++) {
                JOptionPane.showMessageDialog(null, "Please input a number from " + MIN + " to " + MAX + ".");
                JOptionPane.showMessageDialog(null, "Please try again.");
                String userRating3 = JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". ");
                int userRating4 = Integer.parseInt(userRating3);
                if (userRating4 >= MIN && userRating4 <= MAX) {
                    break;
                }
                if (attempts == 3) {
                    JOptionPane.showMessageDialog(null, "Come back later and try again");
                    break;
                }
            }

            JOptionPane.showMessageDialog(null, "Thank you for your rating of Krabby Patties!");
        }
    }
}

In your code, the main problems is that you are not incrementing attempts variable that allows you to break the cycle.

Also you can improve your code by changing the cycle type, and reuse for next attempts the already existing code, and avoid repeating yourself (check DRY concept - Don't Repeat Yourself).

The code can be something like this:

import javax.swing.JOptionPane;

public class LoopLab {

  public static void main(String[] args) {
    int MIN = 1;
    int MAX = 10;
    int attempts = 0;

    do {

      int userRating2 = Integer.parseInt(
          JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". "));

      if (userRating2 >= MIN && userRating2 <= MAX) {
        JOptionPane.showMessageDialog(null, "Thank you for your rating of Krabby Patties!");
        break;
      } else if (++attempts < 3) {
        JOptionPane.showMessageDialog(null, "Please input a number from " + MIN + " to " + MAX + ".");
        JOptionPane.showMessageDialog(null, "Please try again.");
      } else {
        JOptionPane.showMessageDialog(null, "Come back later and try again");
        break;
      }
    } while (true);

  }
}

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