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);*/
//输出一个直角三角形;
/*for (int i = 1;i <= 5 ;i++ ) {
for (int j = 1;j <=i ;j++ ) {
System.out.print("*");
}
System.out.println();
}*/
int i = 1;
int j = 1;
while (i <=5) {
while (j <= i) {
System.out.print("*");
}
i++;
j++;
}
System.out.println();
}
}
|
|