利用while循环,求1-100内所有整数的和、所有奇数的和及所有偶数的和。

2024-11-27 04:20:23
推荐回答(1个)
回答1:

var scount1=0; var scount2=0; var ssum1=0; var ssum2=0; var i =1; while (i<=100){ if(i%2==0){ scount2 +=1; ssum2 +=i; }else{ scount1 +=1; ssum1 += i; } i++; } document.write ("奇数的个数是:"+scount1+"
"); document.write("所有奇数之和为:"+ssum1+"
"); document.write ("偶数的个数是:"+scount2+"
"); document.write("所有偶数之和为:"+ssum2+"
");