示例1:
public class Demo05 {
public static void main(String[] args) {
//1到100之间的数所有的奇数和,偶数和
int addSum=0;
int evenSum=0;
for (int i=0;i<=100;i++){
if(i%2!=0){
addSum+=i;
}else{
evenSum+=i;
}
}
System.out.println("奇数的和:"+addSum);
System.out.println("偶数的和:"+evenSum);
}
}
示例2:
public class Demo06 {
public static void main(String[] args) {
//for循环输出1~1000之间能被5整除的数,并且每行输出3个
// \t表示空格,\n表示换行
for (int i=0;i<=1000;i++){
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println();
//System.out.println("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
示例3:
public class Demo07 {
public static void main(String[] args) {
//九九乘法表
//1.我们先打印第一列
//2.我们把固定的1再用循环包起来
//3.去掉重复项,i<=j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <=j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();//换行
}
}
}
示例4:
public class Demo08 {标签:语句,示例,int,System,循环,println,public,out From: https://www.cnblogs.com/123456dh/p/17089670.html
public static void main(String[] args) {
int[] num={10,20,30,40,50,60};
//遍历数组的元素
for (int i : num) {
System.out.println(i);
}
System.out.println("===============");
for (int i = 0; i < 6; i++) {
System.out.println(num[i]);
}
}
}