首页 > 编程 > Java > 正文

Java三种循环求和方法

2019-11-26 10:14:46
字体:
来源:转载
供稿:网友

注意:100之和为5050

普通for循环:

public class HundredSum {  public static void main(String[] args){   int x=0;   for(int i=1;i<=100;i++){    x=x+i;//x+=i;   }   System.out.print(x);  } } 

while循环:

public class HundredSum {  public static void main(String[] args){   int x=0; int i;   while (i<=100){    x=x+i; //x+=i;   i++;   }   System.out.print(x);  } } 

do-while循环:

public class HundredSum{  public static void main(String[] args){   int i=0,x=0;   do{    x=x+i; //x+=i;   i++;   }while (i<=100); //先循环do语句块,再执行while,不满足while条件则跳出循环   System.out.print(x);  } } 

以上就是本次整理的3种常用的求和方法,感谢大家对武林网的支持。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表