标签:static image ref src 表达式 class while ima java
循环结构:while dowhile
while语法格式:
while(条件表达式)
{
执行语句;
}
int x = 1; while (x<3){ System.out.println("x=" +x); x++; } int a = 1;
练习:sum=1+100=5050
int i=1,sum=0; while(i<=100) { sum=sum+i; i++; System.out.println("sum="+sum);//输出语句写在循环体内 }
输出每次叠加的过程
int i=1,sum=0; while(i<=100) { sum=sum+i; i++; } System.out.println("sum="+sum);//循环体外
运行结果:
package cn.itcast.test; public class While_Demo {// 100以内能被6整除的次数
public static void main(String[] args) { int i=1; int count=0; while(i<=100) { if(i%6==0) count++; i++; } System.out.println("conut="+count); } }
运行结果:
do while语法格式:
do
{
执行语句;
}while(条件表达式);
do { System.out.println("a="+a); a++; }while(a<3);
package cn.itcast.test; public class DoWhile_Demo { public static void main(String[] args) { int i=1,sum=0; do { sum=sum+i; i++; }while(i<=100);//分号切记莫忘 System.out.println("sum="+sum); } }
标签:static image ref src 表达式 class while ima java
原文地址:http://www.cnblogs.com/cao-yin/p/7290747.html