用Java代码编写利用循环求1-100之和
你可以使用for循环来求1-100之和。以下是用Java代码编写的示例:
public class SumOfNumbers {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("The sum of numbers from 1 to 100 is: " + sum);
}
}
运行上述代码将输出:The sum of numbers from 1 to 100 is: 5050。
原文地址: http://www.cveoy.top/t/topic/i5yJ 著作权归作者所有。请勿转载和采集!