Java: Array with loop

后端 未结 9 1064
借酒劲吻你
借酒劲吻你 2020-12-06 04:49

I need to create an array with 100 numbers (1-100) and then calculate how much it all will be (1+2+3+4+..+100 = sum).

I don\'t want to enter these numbers into the a

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 05:06

    If all you want to do is calculate the sum of 1,2,3... n then you could use :

     int sum = (n * (n + 1)) / 2;
    

提交回复
热议问题