Java Scanner Continuous User input?

前端 未结 3 1439
半阙折子戏
半阙折子戏 2021-01-03 05:00

For java practice, i am trying to create a program that reads integers from the keyboard until a negative one is entered.

and it prints the maximum and minimum of th

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-03 05:39

    A simple loop can solve your problem.

        Scanner s = new Scanner(System.in);
        int num = 1;
        while(num>0)
        {
            num = s.nextInt();
            //Do whatever you want with the number
        }
    

    The above loop will run until a negative number is met.

    I hope this helps you

提交回复
热议问题