for结构
package com.struct;
public class FoDemo01 {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a<=100){
System.out.println(a);//循环体
a+=1;//迭代
}
System.out.println("while循环结束");
//初始化;//条件判断;//迭代更新
for(int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
}
}
2
package com.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0~100之间的奇书和偶数的和
int oddsum = 0;
int evensum = 0;
for (int i = 1;i<=100;i++){
if (i%2!=0){
oddsum+=i;
}else{
evensum+=i;
}
}
System.out.println("奇数的和"+oddsum);
System.out.println("偶数的和"+evensum);
}
}
3
package com.struct;
public class ForDemo03 {
public static void main(String[] args) {
//联系2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");// \t表示插入空格
}
if (i % 15 == 0) {
//System.out.println();表示换行
System.out.print("\n");//表示换行
}
}
//println 输出完之后会换行
//print 输出完之后不会换行
}
}
4
package com.struct;
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1;i<=j;i++){
System.out.print(i+"*"+j+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
5
package com.struct;
//增强FOR循环
public class ForDemo05 {
public static void main(String[] args) {
int[] numers = {1,2,3,4,5,6,7,8,9};//定义一个数组
for (int i = 0;i<9;i++){
System.out.println(numers[i]);
}
System.out.println("======================");
//遍历数组的元素
for (int x:numers){
System.out.println(x);
}
}
}