Java - adding value in a while loop

后端 未结 4 1270
栀梦
栀梦 2021-01-27 14:46

this one just is hurting my brain. http://programmingbydoing.com/a/adding-values-in-a-loop.html

Write a program that gets several integers from the user.

4条回答
  •  梦如初夏
    2021-01-27 15:04

    The code will be as below :

     public static void main(String[] args) throws Exception {
         Scanner keyboard = new Scanner(System.in);
         int input = 0;
         int total = 0;
         System.out.println("Start entering the number");
         while((input=keyboard.nextInt()) != 0)
             {   
                 total = input + total;
             }
        System.out.println("The program exist because 0 is entered and sum is "+total);
    }
    

提交回复
热议问题