day03-循环流程-for循环
This commit is contained in:
56
day03/src/com/inmind/for03/Demo07.java
Normal file
56
day03/src/com/inmind/for03/Demo07.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package com.inmind.for03;
|
||||||
|
/*
|
||||||
|
for , while ,do-while 循环
|
||||||
|
循环三要素:循环变量初始化、循环条件、修改循环变量
|
||||||
|
|
||||||
|
for循环的格式:
|
||||||
|
for(循环变量初始化①;循环条件②;修改循环变量④){
|
||||||
|
循环语句③
|
||||||
|
}
|
||||||
|
|
||||||
|
执行顺序:①②③④>②③④>②③④…②不满足为止。
|
||||||
|
|
||||||
|
注意:for循环的循环变量的定义,只作用于当前范围{}
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//要求学生跑100圈
|
||||||
|
/*System.out.println("跑了第1圈");
|
||||||
|
System.out.println("跑了第2圈");
|
||||||
|
System.out.println("跑了第3圈");
|
||||||
|
System.out.println("跑了第4圈");
|
||||||
|
System.out.println("跑了第5圈");*/
|
||||||
|
|
||||||
|
for(int i = 1;i<= 100;i++){
|
||||||
|
System.out.println("跑了第"+i+"圈");
|
||||||
|
}
|
||||||
|
System.out.println("--------------------------------------------------------");
|
||||||
|
//要求学生倒着数:100~1
|
||||||
|
for(int j = 100;j>=1;j--){
|
||||||
|
System.out.println("跑了第"+j+"圈");
|
||||||
|
}
|
||||||
|
System.out.println("--------------------------------------------------------");
|
||||||
|
//打印所有的大小字母:a-z 97-122、A-Z 65-90
|
||||||
|
|
||||||
|
//char类型来实现:(自动类型转换)
|
||||||
|
for (char i = 'a'; i <= 'z'; i++) {
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (char i = 'A'; i <= 'Z'; i++) {
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
System.out.println("---------------------------------");
|
||||||
|
//int类型来实现:(强制类型转换)
|
||||||
|
for (int i = 97; i <= 122; i++) {
|
||||||
|
System.out.println((char) i);
|
||||||
|
}
|
||||||
|
System.out.println("------------------------------");
|
||||||
|
for (int i = 90; i >= 65; i--) {
|
||||||
|
System.out.println((char)i);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user