for (初始化语句;条件判断语句;条件控制语句) {
循环体语句(重复执行的代码);
}
执行流程: 1. 执行初始化语句 2. 执行条件判断语句,看其结果是真还是假 如果是false,循环结束 如果是true,继续执行 3. 执行循环体语句 4. 执行条件控制语句 5. 回到2继续
public static void main(String[] args) {
//用循环输出5次HelloWorld
for (int i = 1; i <= 5; i++) {
System.out.println("Hello World");
}
}
2. 案例1:输出数字15和数字51
public static void main(String[] args) {
//for循环里面的三句话写什么不是固定的代码,根据题目的要求思考出来
//循环输出1~5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
//循环输出5~1
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
}
3. 案例2:输出整数1~10的和
public static void main(String[] args) {
//需求:循环求1 ~ 10的和
//1. 定义接收求和变量
int sum = 0;
//2. for循环求和
for (int i = 1; i <= 10; i++) {
sum += i;
}
//3. 输出结果
System.out.println("和为:" + sum);
}
4. 案例3:循环判断奇偶数
public static void main(String[] args) {
//输出1~100之间所有的数字是奇数还是偶数
// 1. 循环
for (int i = 1; i <= 100; i++) {
// 2. 判断是偶数还是奇数
if (i % 2 == 0) {
System.out.println(i + "是偶数");
} else {
System.out.println(i + "是奇数");
}
}
}
5.案例4:输出1-200之间7的倍数
public static void main(String[] args) {
// 输出1~200之间7的倍数的数字
for (int i = 1; i <= 200; i++) {
if (i % 7 == 0) {
System.out.println(i);
}
}
// 1~200之间7的倍数有几个
// 定义变量统计个数
int count = 0;
for (int i = 1; i <= 200; i++) {
if (i % 7 == 0) {
count++;
}
}
System.out.println("7的倍数有" + count + "个");
}
2.while循环语句
1. 初始化语句
while(循环条件){
循环体语句(重复执行的代码);
迭代语句;
}
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
2. 珠穆朗玛峰案例
public static void main(String[] args) {
//1. 定义变量准备统计折叠次数
int count = 0;
// 定义变量纸的厚度
int paper = 1;
// 用while循环代表多次,纸 <= 88480000
while (paper <= 88480000) {
paper = paper * 2;
count++;
}
System.out.println("需要折叠" + count + "次");
}
3. do…while循环
1. do…while循环语句格式
do{
循环体语句;
条件控制语句;
}while(条件判断语句);
2.循环打印1-10
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
4. 三种循环的区别和使用场景
for
明确循环次数
while
不明确循环次数的情况和无限循环
while(true){
System.out.println("HelloWorld")
}
do...while
一般不用
5. 循环跳转语句
continue :用于跳出当前循环的当次执行,进入下一次循环。
break :跳出并结束当前整个循环。
public static void main(String[] args) {
//for循环输出1-10
for (int i = 1; i <= 10; i++) {
//判断
if(i % 3 == 0){
continue;//跳过本次循环,进入下次循环
// break;//直接结束循环
}
System.out.println(i);
}
}
1. 案例1:输出7倍数以外的数
public static void main(String[] args) {
int count = 0;
for (int i = 1; i < 100; i++) {
if (i % 7 ==0){
continue;
}
System.out.println(i);
}
}
2. 案例2:循环求多个整数的和
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0; //定义一个接收和的变量
while (true) {
System.out.println("请您输入任意整数,结束请输入数字0:");
int number = sc.nextInt(); //键盘录入
if (number == 0) {
break; //输入0结束循环
}
sum += number; //求和
}
System.out.println("您所输入整数的和为:" + sum);
}
6. Random随机数
可以生成一个随机的数字。
步骤: 1.导包 import java.util.Random; 2.创建对象 Random r = new Random(); 3.
public static void main(String[] args) {
// 创建随机数对象
Random r = new Random();
// 生成随机数0~9
int a = r.nextInt(10) ;
System.out.println(a);
// 生成0~5的随机数
int b = r.nextInt(6);
System.out.println(b);
// 生成1~100的随机数
int c = r.nextInt(100) + 1;
System.out.println(c);
}
7. 猜数字小游戏
public static void main(String[] args) {
Random r = new Random();
Scanner sc = new Scanner(System.in);
int number = r.nextInt(100) + 1;
while(true){
System.out.println("请输入一个1~100的数字:");
int guess = sc.nextInt();
if (guess > number){
System.out.println("您输入的数值太大了!");
}else if(guess < number){
System.out.println("您输入的数值太小了!");
}else{
System.out.println("恭喜您猜对了");
break;
}
}
}
总结:
判断语句: if switch 循环: for while do...while 跳转语句: continue break



