Home > Back-end >  How can I make my while loop run correctly?
How can I make my while loop run correctly?

Time:09-28

My code is supposed to ask the user to input a number between 1 and 10. If they input a number between 1 and 10, then it will say thank you and end there. If not, it should loop and say, Please input a number between 1 and 10, and Please try again. It will then make the user input again. My problem is that after the second input, it automatically ends the loop even if they make an incorrect input the second time. By the way, I am very new to coding so please go easy on me in the comments XD. Someone, please help, thanks. Here is my code:

import javax.swing.*;

public class LoopLecture {
    public static void main(String[] args) {
        int MIN = 1;
        int MAX = 10;
        String rating;
        rating = JOptionPane.showInputDialog(null, "Please enter your rating of Krabbypatties on a scale of" 
                  MIN   "to"   MAX   ".With"   MIN   "meaning youhated it and"   MAX   "meaning you loved it.");
        int rating2 = Integer.parseInt(rating);
        while (rating2 < MIN || rating2 > MAX) {
            JOptionPane.showMessageDialog(null, "You must enter a value between "   MIN   "and"   MAX   ".");
                    JOptionPane.showMessageDialog(null, "Please try again.");
            rating = JOptionPane.showInputDialog(null, "Please enter your rating of Krabbypatties on a scale of" 
                      MIN   "to"   MAX   ".With"   MIN   "meaning youhated it and"   MAX   "meaning you loved it.");
            if (rating2 >= MIN && rating2 <= MAX)
                break;
        }
        JOptionPane.showMessageDialog(null, "Thank you!");
    }
}

CodePudding user response:

Within for loop try assigning the new value to the rating2

rating = JOptionPane.showInputDialog(null, "Please enter your rating of Krabby


rating2 = JOptionPane.showInputDialog(null, "Please enter your rating of Krabby

CodePudding user response:

while (true) { // creates infinite while loop


break; // ends an infinite loop


}

You have a break; expression in your if statement, so I'm sure you understand how it works, but try just making the loop infinite, like shown above.

CodePudding user response:

more better may to solve it:

  1. parse rating to rating2 in loop
  2. break if number between 1 and 10
  3. catch NumberFormatException, no need do anything, just ignore it
public static void main(String[] args) {
    int MIN = 1;
    int MAX = 10;
    String rating = JOptionPane.showInputDialog(null, "Please enter your rating of Krabbypatties on a scale of"
              MIN   "to"   MAX   ".With"   MIN   "meaning youhated it and"   MAX   "meaning you loved it.");
    while (true) {
        try {
            int rating2 = Integer.parseInt(rating);
            if (rating2 >= MIN && rating2 <= MAX) {
                break;
            }
        } catch (NumberFormatException ignored) {
        }
        JOptionPane.showMessageDialog(null, "You must enter a value between "   MIN   "and"   MAX   ".");
        JOptionPane.showMessageDialog(null, "Please try again.");
        rating = JOptionPane.showInputDialog(null, "Please enter your rating of Krabbypatties on a scale of"
                  MIN   "to"   MAX   ".With"   MIN   "meaning youhated it and"   MAX   "meaning you loved it.");
    }
    JOptionPane.showMessageDialog(null, "Thank you!");
}
  • Related