Loop user input until conditions met

前端 未结 3 1736
盖世英雄少女心
盖世英雄少女心 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
    

提交回复
热议问题