0
Scanner userIn = new Scanner(System.in);                                        
System.out.println("enter number");                                     
int no = userIn.nextInt();                                      
while (no > 20)                                     
{                                       
  System.out.println("too big");                                        
  no = userIn.nextInt();                                        
  {                                     
    if (no <= 20)                                       
    {                                       
      for (int i=0; i < no; i++)                                        
      {                                     
        System.out.println(i+1);                                        
      }                                     
    }                                       
  }                                     
}

I am sorry to ask such a silly question, but I am starting to learn programming and I can't put my head around tutorials. I am trying to code where if a given input if larger than 20, it will give the output of "too big" and ask you to enter a number again until it is 20 or less. If the number is 20 or less, it will count from 1 to the chosen number from keyboard. Why does this only work when I enter a number that is large than 20 and after, but not straight away after compiling, is it wrong position, may I ask how I can resolve this? Thank you.

fdasf
  • 31
  • 3
  • Why do you have another block after `no = userIn.nextInt();`? End the loop there. Your `if` is a bad idea in the `while` loop. – Elliott Frisch Feb 05 '18 at 00:19
  • I'd also consider using a `do {...} while (no > 20);` instead and prompt the user from within the loop – MadProgrammer Feb 05 '18 at 00:21
  • I wanted it to ask me the same question again if the input was larger than 20, if I remove that, it doesn't repeat. Oh right I see, what loop would be best if I may ask? – fdasf Feb 05 '18 at 00:22

2 Answers2

2

Simply putting if your number(no) is less than 20, You will never get inside the while loop. Hence it will never execute your for loop inside the while loop.

do it something like this

Scanner userIn = new Scanner(System.in);                                        
System.out.println("enter number");                                     
int no = userIn.nextInt();                                      
while (no > 20)                                     
{                                       
  System.out.println("too big");                                        
  no = userIn.nextInt();                           
}
 for (int i=0; i < no; i++)                                        
      {                                     
        System.out.println(i+1);                                        
      }                                     
Yohannes Gebremariam
  • 1,967
  • 2
  • 15
  • 23
  • Oh I see now, because the for loop is out of the while loop, it can now execute beforehand lol opps, thank you Yohannes :) – fdasf Feb 05 '18 at 00:45
  • you welcome. accept it if you find it helpful. – Yohannes Gebremariam Feb 05 '18 at 00:46
  • just out of curiosity, do I have to use a do while to have the opposite effect? meaning, if I enter a number less than 21, it will count from 1 to the number and ask again, until a number larger than 20 is inputted which will end the program? – fdasf Feb 05 '18 at 00:47
  • I can't see that is possible. Do while loop is like while loop. the only difference is do while executed at least once regardless how the logic control evaluated. – Yohannes Gebremariam Feb 05 '18 at 00:49
0

you can read infinity till the user enter a number less than 20

 Scanner userIn = new Scanner(System.in);
            System.out.println("enter number");
            int no;
            while ((no = userIn.nextInt()) > 20) {
                System.out.println("too big");
             // the while loop will stop when a number less than 20 is entered
            }

            for (int i = 0; i < no; i++) {
                System.out.println(i + 1);
            }

TEST: enter image description here

Bishoy Abd
  • 2,116
  • 1
  • 13
  • 26