day03-流程控制语句-循环流程-for循环-练习
This commit is contained in:
@@ -27,8 +27,22 @@ public class Demo07 {
|
||||
System.out.println("程序结束");
|
||||
|
||||
//请使用for,循环打印1~10
|
||||
for (int j = 1; j <= 10; j++) {
|
||||
System.out.println(j);
|
||||
}
|
||||
//请使用for,循环打印10~1
|
||||
//请使用for,循环打印A~B
|
||||
for (int k = 10; k > 0; k--) {
|
||||
System.out.println(k);
|
||||
}
|
||||
System.out.println("--------------------");
|
||||
//请使用for,循环打印A~Z 65:A Z:90
|
||||
for (int i = 65; i < 91; i++) {
|
||||
System.out.println((char) i);
|
||||
}
|
||||
|
||||
System.out.println("--------------------");
|
||||
for (char c = 'A'; c <= 'Z'; c++) {
|
||||
System.out.println(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
day03/src/com/inmind/for03/Test08.java
Normal file
27
day03/src/com/inmind/for03/Test08.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.inmind.for03;
|
||||
//循环流程-for循环_偶数累和
|
||||
public class Test08 {
|
||||
//使用循环,计算1-100之间的偶数和
|
||||
/*
|
||||
实现步骤:
|
||||
1.定义一个变量,用来记录总和
|
||||
2.使用for实现1~100的值的变化
|
||||
3.在for的循环体中,判断对应的值是否是偶数,如果是,则累加到总和变量中
|
||||
4.循环结束,打印最终结果
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//1.定义一个变量,用来记录总和
|
||||
int sum = 0;
|
||||
//2.使用for实现1~100的值的变化
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
//3.在for的循环体中,判断对应的值是否是偶数,如果是,则累加到总和变量中
|
||||
if (i % 2 == 0) {//判断是否是偶数
|
||||
// sum = sum + i;
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
//4.循环结束,打印最终结果
|
||||
System.out.println("1~100的偶数和为:"+sum);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user