循环结构
循环语句
组成:
- 初始化语句:一条或多条语句,这些语句完成一些初始化操作。
- 判断条件语句:这是一个boolean表达式,这个表达式能决定是否执行循环体。
- 循环体语句:我们要多次做的事情。
- 控制条件局域:在一次循环结束后,下一次循环判断条件执行前执行。通过用于控制循环条件中的变量,使得循环在合适的时候结束。
for循环
格式:
for循环语句的格式:
for(初始化语句;判断条件语句;控制条件语句) {
循环体语句
}
执行流程:
- 执行初始化语句
- 执行判断条件语句,看其结果是true还是false。如果是false就结束循环;如果是true就继续执行
- 执行循环体语句
- 执行控制条件语句
- 回到第二步继续
案例:
public class t01for {
public static void main(String[] args) {
//需求:在控制台输出5次helloworld
for(int i=1;i <= 5; i++) {
System.out.println("helloworld");
}
}
}
练习
练习1:
需求:获取数据1-5和5-1
public class t02for {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
for(int j = 5; j >= 1; j--) {
System.out.println(j);
}
}
}
练习2:
需求:求出1-5之间的数据和
分析:
- 定义一个求和变量,初始化值为0
- 获取1-5的数据,用for循环实现
- 把每一次获取到的数据累加到求和变量
- 输出求和变量
public class t03for {
public static void main(String[] args) {
int sum = 0;
for(int i = 1;i <=5; i++) {
sum = sum + i; //可以写为 sum +- x;
}
System.out.println("sum:" + sum);
}
}
练习3:
需求:求出1-100之间的偶数和
分析:
- 定义求和变量,初始化值为0
- 获取1-100之间的偶数,用for循环实现
- 拿到每一个获取的数进行判断看是否是偶数。如果是偶数就累加
- 输出求和变量
public class t04for {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; i++) {
if(i%2 == 0) {
sum = sum + i;
}
}
System.out.println("sum:" + sum);
}
}
练习4:
需求:在控制台输出所有的“水仙花数”
水仙花数是指一个三位数,其个位数字的立方和等于该数本身。举例:153
分析:
- 三位数其实是告诉了我们范围,这个用for循环实现
- 获取每一个三位数的个位,十位,百位
获取一个三位数的个位,十位,百位:
个位:153%10
十位:153/10%10
百位:153/10/10%10 - 拿个位,十位,百位的立方和和该数本身进行比较。如果相等,就说明该数是水仙花数,在控制台打印。
public class t05for {
public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == i) {
System.out.println(i);
}
}
}
}
练习5:
需求:统计水仙花数共有多少个
分析:
- 定义统计变量,初始化值为0
- 获取三位数,用for循环实现
- 获取每个位上的数据
- 判断数据是否是水仙花数。如果是,就统计变量++
- 输出统计变量
public class t05for {
public static void main(String[] args) {
int n = 0;
for (int i = 100; i <= 999; i++) {
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == i) {
n++;
}
}
System.out.println("number:" + n);
}
}
while循环
格式:
while循环的语句格式:
while(判断条件语句) {
循环体语句;
}
完整格式:
初始化语句;
while(判断条件语句) {
循环体语句;
控制条件语句;
}
案例:
public class t06while {
public static void main(String[] args) {
//在控制台输出5次helloworld
int i = 1;
while(i <= 5) {
System.out.println("helloworld");
i++;
}
}
}
练习
需求:求出1-100之间的数据和
public class t06while {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while(i <= 100) {
sum += i;
i++;
}
System.out.println("sum:" + sum);
}
}
do…while循环
格式:
do...while循环语句的格式:
do {
循环体语句;
}while(判断条件语句);
完整格式:
初始化语句;
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
执行流程:
- 执行初始化语句
- 执行循环体语句
- 执行控制条件语句
- 执行判断条件语句,看是true还是false。如果是false,就结束循环;如果是true,就回到B继续。
案例:
public class t07dowhile {
public static void main(String[] args) {
//在控制台输出5次helloworld
int i = 1;
do {
System.out.println("helloworld");
i++;
}while (i <= 5);
}
}
三种循环的区别
三种循环可以完成相同的事情,但是也是有小区别的:
- do…while循环语句至少执行一次循环体
- 而for和while循环语句要先进行条件的判断,然后看是否执行循环体语句。
public class t08dowhile {
public static void main(String[] args) {
int x = 3;
do {
System.out.println("helloworld");
x++;
}while(x < 3);
//输出helloworld
}
}
public class t08dowhile {
public static void main(String[] args) {
int x = 3;
while(x < 3) {
System.out.println("helloworld");
x++;
}
//无输出结果
}
}
for循环和while循环的小区别:
- for循环结束后,初始化的变量不能被使用了
- 而while循环结束后,初始化的变量还可以继续被使用
public class t08dowhile {
public static void main(String[] args) {
for(int x = 1; x <= 5; x++) {
System.out.println("helloworld");
}
System.out.println(x);
//报错
}
}
public class t08dowhile {
public static void main(String[] args) {
int x = 1;
while(x <= 5) {
System.out.println("helloworld");
x++;
}
System.out.println(x);
//输出5个helloworld和6
}
}
推荐使用的顺序:for - - while - - do…while
循环嵌套
循环嵌套:循环体语句本身是一个循环语句
案例:
需求:请输出一个4行5列的星星(*)图案
public class t09 {
public static void main(String[] args) {
//在一行上输出一颗*
//System.out.println("*");
//在一行上输出5颗*
//System.out.println()每次把内容输出后加一个换行
//解决方案:
//用System.out.print()可以解决
/*
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
*/
/*
//用循环改进代码,第一行五颗*
for(int x = 1; x <= 5; x++) {
System.out.print("*");
}
//采用下面的内容可以实现换行
System.out.println();
//第二行的五颗*
for(int x = 1; x <= 5; x++) {
System.out.print("*");
}
System.out.println();
//第三行的五颗*
for(int x = 1; x <= 5; x++) {
System.out.print("*");
}
System.out.println();
//第四行的五颗*
for(int x = 1; x <= 5; x++) {
System.out.print("*");
}
System.out.println();
*/
//重复的代码执行多次,用循环改进
for(int y = 1; y <= 4; y++) {
for(int x = 1; x <= 5; x++) {
System.out.print("*");
}
System.out.println();
}
//结论:外循环控制的是行,内循环控制的是列
}
}
练习
练习1
需求:请输出如下图形
*
**
***
****
*****
public class t10 {
public static void main(String[] args) {
//通过简单观察,我们发现这是一个5行的,列数变化的形状
//首先实现一个5行5列的形状
for(int x = 1; x <= 5; x++) {
for(int y = 1; y <= 5; y++){
System.out.print("*");
}
System.out.println();
}
//虽然我们实现了一个5行5列的形状
//但我们要的形状是列数变化的
//第一行:1列 y<=1;第二行:2列 y<=2;第三行:3列 y<=3;第四行:4列 y<=4;第五行:5列 y<=5
//我们需要一个变量是从1开始,到5结束
int z = 1;
for(int x = 1; x <= 5; x++) {
for(int y = 1; y <= z; y++){
System.out.print("*");
}
System.out.println();
z++;
}
//现在实现了需求,但继续观察发现
//x也是一个从1开始,到5结束的变量
//这样就可以省略z,直接用x替代
for(int x = 1; x <= 5; x++) {
for(int y = 1; y <= x; y++){
System.out.print("*");
}
System.out.println();
}
}
}
练习2
需求:在控制台输出九九乘法表
public class t11 {
public static void main(String[] args) {
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "x" + x + "=" + x*y + "\t");
}
System.out.println();
}
// \t:转移字符,表示一个tab键的位置
}
}
跳转控制语句
我们想让循环在某一步结束或者跳过某些数据不要,现在就做不了这件事情。为了弥补这个缺陷,Java就提供了break,continue来实现控制语句的中断和跳转
break
break:中断的意思
使用场景:
- switch语句中用于结束switch语句
- 循环语句中,用于结束循环
public class t12break {
public static void main(String[] args) {
//break可以用于结束当前的循环
for(int x=1; x<=5; x++) {
if (x == 3) {
break;
}
System.out.println("helloworld");
}
//如果是多层循环
//break结束的是离他最近的那个循环
//如果要跳出外层循环
//带标签的语句:
//格式:标签名:语句
wc:for(int x=1; x<=3; x++) {
nc:for(int y=1; y<=4; y++) {
if(y == 2) {
break wc;
}
System.out.print("*");
}
System.out.println();
}
}
}
如何使用:
- 跳出单层循环
- 跳出多层循环
用带标签的语句格式
continue
continue:循环的意思
使用场景:循环中。离开使用场景是没有意义的
public class t12continue {
public static void main(String[] args) {
for(int x=1; x<=5; x++) {
if(x == 3) {
continue;
}
System.out.println("helloworld"+x);
}
//continue 跳出这一次的循环,进行下一次
}
}
break和continue的区别:
- break:跳出整个循环
- continue:跳出这一次的操作,进行下一次的执行
随机数Random
Random:用于产生随机数的类。用法和Scanner类似。
使用步骤:
- 导包
import java.util.Random; - 创建对象
Random r = new Random(); - 获取随机数
int number = r.nextInt(10);
获取的范围:[0,10) 包括0,不包括10
public class t13random {
public static void main(String[] args) {
Random r = new Random();
for(int x=1; x<=10; x++) {
int number = r.nextInt(10);
System.out.println("number:"+number);
}
//获取一个1-100的随机数
int i = r.nextInt(100)+1;
System.out.println(i);
}
}
练习
需求:猜数字小游戏。系统产生一个1-100之间的随机数,请猜出这个数据是多少。
分析:
- 系统产生一个1-100之间的随机数
- 键盘录入我们要猜的数据
- 比较两个数据,看我们猜的是否正确
如果大了,提示:你猜的数大了;
如果小了,提示:你猜的数小了;
如果相等,提示:恭喜你,猜中了 - 为了实现多次猜数据,需要加入循环,而我们又不知道猜多少次能中。
死循环:while(true) {…}
for(; ; ) {…}
public class t13random {
public static void main(String[] args) {
Random r = new Random();
//获取一个1-100的随机数
int i = r.nextInt(100)+1;
Scanner sc = new Scanner(System.in);
//多次猜数据
while (true) {
System.out.println("请输入你要猜的整数(1-100)");
int x = sc.nextInt();
if(x > i) {
System.out.println("你猜的数" + x + "大了");
}else if (x < i) {
System.out.println("你猜的数" + x + "小了");
}else {
System.out.println("恭喜你答对了");
break; //跳出循环
}
}
}
}