Files
javaSE251223/day03/src/com/inmind/Demo03_for.java

78 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind;
/*
forwhiledo-while 循环
循环三要素:循环变量初始化、循环条件、修改循环变量
for循环的格式
for(循环变量初始化①;循环条件②;修改循环变量④){
循环体语句③;
}
执行顺序:①②③④>②③④>②③④…②不满足为止。
*/
public class Demo03_for {
public static void main(String[] args) {
//for循环练习使用循环计算1-100之间的偶数和
/*
1.定义个变量用来保存总和的结果
2.for循环1~100判断出所有偶数
3.将每个偶数相加得到结果打印
*/
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
//当前i一定是偶数
// sum = sum+i;
sum+= i;
}
}
System.out.println("1-100之间的偶数和:"+sum);
}
//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() {
//去跑圈要求你跑1000圈
/*System.out.println("第1圈");
System.out.println("第2圈");
System.out.println("第3圈");*/
for(int i = 1;i<=3;i++){
System.out.println(""+i+"");
}
System.out.println("程序结束");
}
}