day03-.for循环的语法&练习

This commit is contained in:
2025-12-24 15:35:54 +08:00
parent 6517abd581
commit 3f09026cb7

View File

@@ -13,12 +13,40 @@ for(循环变量初始化①;循环条件②;修改循环变量④){
*/
public class Demo03_for {
public static void main(String[] args) {
//for循环练习使用循环计算1-100之间的偶数和
}
//for循环练习1
public static void forDemo1() {
/*
打印1~10的值
打印10~1的值
打印所有的字母a-z、A-Z
*/
//打印1~10的值
for(int a = 1;a<=10;a++){
System.out.println(a);
}
System.out.println("---------------");
//打印10~1的值
for(int b = 10;b>=1;b--){
System.out.println(b);
}
System.out.println("---------------");
//打印所有的字母a-z、A-Z(注意字符在java运算时根据ASCII码表对应的十进制的值计算)
//a-z 97 A-Z 65 0-9 48
for(int i = 97;i<=122;i++){
System.out.println((char) i);//4字节的int转为2字节的char
}
System.out.println("---------------");
for (int j = 'A'; j <= 'Z'; j++) {//自动类型转换
System.out.println((char) j);//4字节的int转为2字节的char强制类型转换
// System.out.println(j);//4字节的int转为2字节的char
}
}
//for循环的语法学习
public static void forMethod() {