1.while循环
//1+2+3···+100求和
package HelloWorld;
public class whileDome {
public static void main(String[] args) {
int i=0 ,sum=0;
while(i<=100) {
sum +=i;
i++;
}
System.out.println(sum);
}
}
2.do ····while循环
//1+2+3···+100求和
package HelloWorld;
public class whileDome {
public static void main(String[] args) {
int i=0 ,sum=0;
do{
sum +=i;
i++;
}while(i<=100);
System.out.println(sum);
}
}
do···while与while 区别
dowhile:先循环一次在判断
while:先判断后循环
3.for循环
package HelloWorld;
public class forDome {
public static void main(String[] args) {
for(int i=1;i<10;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j+"*"+i+"="+i*j+" \t");
}
System.out.println();
}
}
}
4.foreach
package HelloWorld;
public class foreachDome {
public static void main(String[] args) {
int[] arrs=new int[] {10,20,30,40,50,60};
for(int ele:arrs){
System.out.println(ele);
}
}
}