- while循环语句
public class Example12 {
public static void main(String[] args) {
int x=1;
while(x<=4) {
System.out.println(x);
x++;
}
}
}
- do…while
和(while)比起来do…while无论如何都可以执行一次,而while如果条件不成立则一次也不执行
public class Example13 {
public static void main(String[] args) {
int x=1;
do {
System.out.println(x);
x++;
}while(x<=4);
}
}
- for循环语句
public class Example14 {
public static void main(String[] args) {
int sum=0;
for(int x=0;x<=10;x++){
sum+=x;
}System.out.println("和的值"+sum);
}
}
- for嵌套循环(感觉挺好用的算法题用得比较多)
public class Example16 {
public static void main(String[] args) {
int i,j;
for(i=1;i<=9;i++){
for(j=1;j<i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
}