Java Scanner Continuous User input?

前端 未结 3 1437
半阙折子戏
半阙折子戏 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:35

    You could do something like:

    Scanner input = new Scanner(System.in);
    int num;
    while((num = input.nextInt()) >= 0) {
        //do something
    }
    

    This will make num equal to the next integer, and check if it is greater than 0. If it's negative, it will fall out of the loop.

提交回复
热议问题