-
cxzchange
public class test {
public static void main(String[] args) {
int total = sum(1,1000);
System.out.println(total);
}
public static int sum(int start, int end) {
int total = 0;
start = 1;
while (start <= end) {
total += start;
start++;
}
return total;
}
}public class test {
public static void main(String[] args) {
int total = 0;
int i = 1;
while (i <= 1000) {
total += i;
i++;
}
System.out.println(total);
}
}
-
XhstormR
public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 1000; i++) {
sum += i;
}
System.out.println(sum);
}
}代码以上,望采纳。
-
cyc川
public class while_1 {
public static void main(String[] args) {
int i=1;//从i开始
int sum=0;//表示和
int j=1000;//到j结束
while(i<=j){
sum=sum+i;
i++;
}
System.out.println(sum);
}
-
受戒人
//while
var sum=0;
var i = 1;
while(i<=1000){
sum+=i;
++i;
}
console.log(sum);
alert(sum);//for
var sum = 0;
for(int i = 1; i <= 1000; i++)
{
sum += i;
}
-
阿旭_
public class Test11 {
public static void main(String[] args) {
int i = 1, n = 0;
while (i <= 1000) {// 如何用while循环计算1至1000之和,并将之和赋值给变量?
n = (i == 1000) ? (System.out.printf("1+2+……+100=%d", n += i++) != null ? n : 0) : (n + i++);
}
}
}
-
2760889966649
public class test{ public static void main(String[]args){ int i=1; int sum=0; while(i<=1000){ sum=i+sum; i++; } System.out.println("1+2+3+...+1000的和是:"+sum); } }
-
qq_抱嫦娥O烤玉兔_03445798
public void test2() { int start = 1; int end = 1000; int total = 0; while (start <= end) { total += start; start++; } System.out.println(total); }
-
轻键快码
function sum(num) { var result = 0; var i = 1; while (i <= num) { result += i; i++; } return result; }sum(1000);
-
JustWannaHugU
public class Calculate{ public static void main(String[] args) { int sum = 0; int i=1; while(i<=1000) { sum+=i;++i; } System.out.println(sum); }}