Loop user input until conditions met

前端 未结 3 1724
盖世英雄少女心
盖世英雄少女心 2020-11-29 12:44

I need to ask the user to input a number to be used as the start of a range, and then input another number that is the end of the range. The start number has to be 0 or grea

相关标签:
3条回答
  • 2020-11-29 13:15

    Easy with do-while:

    Scanner keyboard = new Scanner(System.in);
    int startr, endr;
    boolean good = false;
    do
    {
      System.out.println("Enter the Starting Number of the Range: ");
      startr = keyboard.nextInt();
      if(startr % 10 == 0 && startr >= 0)
        good = true;
      else
        System.out.println("Numbers is not divisible by 10");
    }
    while (!good);
    
    good = false;
    do
    {
        System.out.println("Enter the Ending Number of the Range: ");
        endr = keyboard.nextInt();
        if(endr % 10 == 0 && endr <= 1000)
          good = true;
        else
          System.out.println("Numbers is not divisible by 10");
    }
    while (!good);
    
    // do stuff
    
    0 讨论(0)
  • 2020-11-29 13:22

    You need to use a while, something like:

    while conditionsMet is false
        // gather input and verify
        if user input valid then
            conditionsMet = true;
    end loop
    

    should do it.

    0 讨论(0)
  • 2020-11-29 13:23

    The all-purpose procedure is:

    1. Read the input in an infinite loop.
    2. Use a break; statement to exit the loop when the conditions are met.

    Example:

    Scanner keyboard = new Scanner(System.in);
    int startr, endr;
    
    for (;;) {
        System.out.println("Enter the starting number of the range: ");
        startr = keyboard.nextInt();
        if (startr >= 0 && startr % 10 == 0) break;
        System.out.println("Number must be >= 0 and divisible by 10.");
    }
    
    for (;;) {
        System.out.println("Enter the ending number of the range: ");
        endr = keyboard.nextInt();
        if (endr <= 1000 && endr % 10 == 0) break;
        System.out.println("Number must be <= 1000 and divisible by 10.");
    }
    

    If after invalid input you want to display just the error message without repeating the initial prompt message, move the initial prompt message just above/outside the loop.

    If you do not have need for the separate error message, you can re-arrange the code to use a do-while loop to check the conditions, which is just a little shorter:

    Scanner keyboard = new Scanner(System.in);
    int startr, endr;
    
    do {
        System.out.println("Enter the starting number of the range.");
        System.out.println("Number must be >= 0 and divisible by 10: ");
        startr = keyboard.nextInt();
    } while (!(startr >= 0 && startr % 10 == 0));
    
    do {
        System.out.println("Enter the ending number of the range.");
        System.out.println("Number must be <= 1000 and divisible by 10: ");
        endr = keyboard.nextInt();
    } while (!(endr <= 1000 && endr % 10 == 0));
    
    0 讨论(0)
提交回复
热议问题