标题: while语句 [打印本页] 作者: yw201605 时间: 2016-7-2 22:55 标题: while语句 class Demo2_While {
public static void main(String[] args) {
//求1到100的和;
/*int i = 1;
int count = 0;
while (i <= 100) {
count += i;
i ++; //让变量i自增
}
System.out.println("count=" + count);*/
/*for (int x =1;x <= 4 ;x ++ ) {
for (int y =1;y <= 5 ;y ++ ) {
System.out.print("*");
}
System.out.println();
}*/
//用while语句计算1到100的偶数,奇数和;
/*int sum = 0;
int sun = 0;
int x = 1;
while (x <= 100) {
if (x % 2 ==0) {
sum += x;
} else {
sun += x;
}
x ++;
}
System.out.println("sum=" + sum + ",sun=" + sun);*/
//用do while 语句计算1到100的偶数,奇数和;
/*int x = 1;
int sum = 0;
int sun = 0;
do {
if (x % 2 == 0) {
sum += x;
} else {
sun += x;
}
x ++;
}
while (x <= 100);
System.out.println("sum="+ sum + ",sun=" + sun);*/