分别使用循环语句for、while、repeat实现求从1加到100的和
package com。kk。print;
/** 分别使用循环语句for、while、repeat实现求从1加到100的和 */
public class ForWhiledoWhile {
public static void main(String[] args) {
int result1 = 0;
int result2 = 0;
int result3 = 0;
for (int i = 0; i <= 100; i ) {
result1 = i;
}
int k = 0;
while (k <= 100) {
result2 = k;
k ;
}
int j = 0;
do {
result3 = j;
j ;
} while (j <= 100);
System。
out。println("for : " result1 " while : " result2
" do-while : " result3);
}
}。
答:1.正确选定循环变量的初值。比如,对累加问题和连乘问题,变量初值不正确,便影响最终结果。例如计算m=1!+2!+…+10! m=0 n=1 DO20I=1,10...详情>>