2021年1月15日星期五

Java while loop until conditions are met

Trying to make the user keep inputting correct integers between 2 sets of values, but for example if the user inputs a wrong integer, then the while loop skips and runs onto the next code in my program. If anyone could point out how I can make the user repeat the question at each step if they get it wrong, would appreciate it.

System.out.print("How many asteroids: ");      int asteroid = input.nextInt();      while (asteroid > 0) {          System.out.print("x location of the asteroid (between 1-950 pixels): ");          double asteroidLocationX = input.nextDouble();          if (asteroidLocationX >= 1 && asteroidLocationX <= 950) {              System.out.print("y location of the asteroid (between 150-550 pixels): ");              double asteroidLocationY = input.nextDouble();              if (asteroidLocationY >= 150 && asteroidLocationY <= 550) {                  System.out.print("Width of the asteroid (min: 30 pixels, max: 50 pixels): ");                  double asteroidSizeWidth = input.nextDouble();                  if (asteroidSizeWidth >= 30 && asteroidSizeWidth <= 50) {                      System.out.print("Height of the asteroid (min: 30 pixels, max: 50 pixels): ");                      double asteroidSizeHeight = input.nextDouble();                      if (asteroidSizeHeight >= 30 && asteroidSizeHeight <= 50) {                          gc.setFill(Color.ALICEBLUE);                          gc.fillOval(asteroidLocationX, asteroidLocationY, asteroidSizeWidth, asteroidSizeHeight);                      } else                          System.out.println("Wrong input, try again");                  } else                      System.out.println("Wrong input, try again");              }else                  System.out.println("Wrong input, try again");          } else              System.out.println("Wrong input, try again");          asteroid--;      }  
https://stackoverflow.com/questions/65745529/java-while-loop-until-conditions-are-met January 16, 2021 at 09:06AM

没有评论:

发表评论