文章目录
本次任务是记录关于Java基础语法中的循环知识笔记
前言
话不多说,开始干!!为生命而怒吼吧!年轻人~~
提示:以下是本篇文章正文内容,下面案例可供参考
一、深入了解for循环
在上一节的学习中我们已经对“for循环”有了一些初步的了解了,本次任务我们将深入学习“for循环”的同时来了解新的循环(while、do–while)
(1)、for循环的案例
案例一:遍历1~10的数字
public class Test01{
public static void main(String[] args){
//遍历数字 1--10
for(int i=1;i<=10;i++){
System.out.print(i+"、"); //1、2、3、4、5、6、7、8、9、10
}
}
}
(2)、for循环嵌套(通过打印三角形的案例来说明)
//需求1:打印直角三角形
*
**
***
****
*****
for(int i = 0;i<5;i++){
for(int j = 0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
//需求2:打印倒直角三角形
*****
****
***
**
*
for(int i = 0;i<5;i++){
for(int j = 5;j>i;j--){
System.out.print("*");
}
System.out.println();
}
//需求3:打印反直角三角形
*****
****
***
**
*
for(int i = 0;i<5;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 5;j>i;j--){
System.out.print("*");
}
System.out.println();
}
//需求4:打印等腰三角形
*
***
*****
for(int i = 0;i<3;i++){
for(int k = 2;k>i;k--){
System.out.print(" ");
}
for(int j = 0;j<i*2+1;j++){
System.out.print("*");
}
System.out.println();
}
//需求5:打印空心的等腰三角形
*
* *
*****
for(int i = 0;i<3;i++){
for(int k = 2;k>i;k--){
System.out.print(" ");
}
for(int j = 0;j<i*2+1;j++){
if(i==0 || i==2 || j==0 || j==i*2){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
//需求6:打印倒的等腰三角形
*****
***
*
for(int i = 0;i<3;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 5;j>=i*2+1;j--){
System.out.print("*");
}
System.out.println();
}
//需求7:打印倒空心等腰三角形
*****
* *
*
for(int i = 0;i<3;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 5;j>=i*2+1;j--){
if(i==0 || i==2 || j==5 || j==i*2+1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
//需求8:打印 九九乘法表
1×1=1
* 1×2=2 2×2=4
* 1×3=3 2×3=6 3×3=9
* 1×4=4 2×4=8 3×4=12 4×4=16
* 1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
* 1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
* 1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
* 1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
* 1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
for(int i = 1;i<=9;i++){
for(int j = 1;j<=i;j++){
System.out.print(j + "x" + i + "=" + (i*j) + "\t");
}
System.out.println();
}
for(int i = 1;i<=9;i++){
for(int k = 1;k<i;k++){
System.out.print("\t");
}
for(int j = i;j<=9;j++){
System.out.print(i + "x" + j + "=" + (i*j) + "\t");
}
System.out.println();
}
小总结:
以上案例均是体现了for循环嵌套思想而打印出来的图形,在以后的开发工作中虽然很少使用到,但是这是一种锻炼思维逻辑的练习,学习最低标准就是打印出最基本的两个三角形和九九乘法表即可
二、while 循环:
(1)、案例演示
public class Test02{
public static void main(String[] args){
/**
知识点:while循环
语法结构:
while(表达式){
...代码块...
}
理解:
表达式的结果必须是boolean
true - 执行代码块
false- 跳出循环
死循环:
while(true){
System.out.println("死循环");
}
做实验://不会执行,因为表达式无法通过
while(false){
System.out.println("拼命学习");
}
while循环变形记:while循环可以表示for循环
int i = 0;
while(i < 5){
System.out.println("用良心做教育");
i++;
}
案例:我有个梦想,每月存3000,每年递增1000元,多少个月后存满20万
*/
int money = 3000;
int allMoney = 0;
int month = 0;
while(allMoney < 200000){
allMoney+=money;
month++;
if(month % 12 == 0){
money+=1000;
}
}
System.out.println(month + "个月以后存满20万 " + money);
/**
总结:循环次数不确定
*/
}
}
小总结:
当循环次数不定时可以考虑 ,while循环,其次,虽然理论上我们要避免 死循环,但是有些时候死循环也是有好处的。
do…while循环
(1)案例演示
import java.util.Scanner;
public class Test03{
public static void main(String[] args){
/**
知识点:do-while循环
语法结构:
do{
...代码块...
}while(表达式);
理解:
表达式的结果必须是boolean
true - 执行代码块
false- 跳出循环
执行顺序:
1.先执行一遍代码块
2.判断表达式
2.1 true - 执行代码块,再重复第2个步骤
2.2 false- 跳出循环
死循环:
do{
System.out.println("死循环");
}while(true);
案例:学生参加学校组织的歌咏比赛,
大赛在即,老师建议:先彩排一次,如果很令人满意,
以后就不用彩排了,否则每天都排,直到现场表现满意为止!
*/
Scanner scan = new Scanner(System.in);
String str;
do{
System.out.println("许川:旋转、跳跃,我闭着眼~~~");
System.out.println("许川:何老师,您满意了吗?");
str = scan.next();
}while(str.equals("不满意"));
}
}
/**
总结:循环次数不确定
for vs while vs do-while
语法结构的区别:
for(初始化变量;判断条件;更新变量){}
while(判断条件){}
do{}while(判断条件);
循环共同点:判断条件的结果都是boolean值,true-循环 false-跳出循环
执行顺序的区别:
for:先判断,再执行
while:先判断,再执行
do-while:先执行一遍,再判断
应用场景的区别:
循环次数确定:for
循环次数不确定,先判断,再执行:while
循环次数不确定,先执行一遍,再判断:do-while
*/
特殊流程控制语句
break语句
public static void main(String[] args){
/**
知识点:特殊流程控制语句
1.break
作用:作用于循环中,表示跳出当前循环
做实验:
while(true){
System.out.println("111");
System.out.println("222");
if(true){
break;
}
System.out.println("333");
System.out.println("444");
}
案例:循环录入麻生希同学5门课的成绩并计算平均分,
如果某分数录入为负停止录入并提示。
Scanner scan = new Scanner(System.in);
double sum = 0;
boolean flag = true;//true-5次正常录入 false-有输入负数的情况
for(int i = 1;i<=5;i++){
System.out.println("请输入第" + i + "门成绩:");
double score = scan.nextDouble();
if(score < 0){
flag = false;
break;
}
sum+=score;
}
if(flag){
double avg = sum/5;
System.out.println("平均分为:" + avg);
}else{
System.out.println("分数为负数,停止录入");
}
}
continue语句
2.continue
作用:作用于循环中,表示跳过当前循环剩余的部分,进入到下一次循环
做实验:
while(true){
System.out.println("111");
System.out.println("222");
if(true){
continue;
}
System.out.println("333");
System.out.println("444");
}
案例:循环录入Java课5名学生的成绩,
统计分数大于等于80分的学生比例。
Scanner scan = new Scanner(System.in);
int num = 0;//记录成绩大于等于80分学生的人数
for(int i = 1;i<=5;i++){
System.out.println("请输入第" + i + "名学生的成绩:");
double score = scan.nextDouble();
if(score < 80){
continue;
}
num++;
}
System.out.println("分数大于等于80分的学生比例为:" + (num/5.0*100) + "%");
}
return语句
作用:
用于终止该方法
3.return
作用:作用于方法中,表示结束该方法
做实验:
System.out.println("111");
System.out.println("222");
if(true){
return;
}
System.out.println("333");
System.out.println("444");
lable的用法
说明:
lable 是一个标签,可用于给循环结构取名字,从而达到方便控制循环的中断
import java.util.Scanner;
public class Test04 {
/**
* 编写一个程序,最多接收10个数,求这些数的和,(15分)
注意:中途用户可以通过输入886终止程序,并显示输入数的和
( 很遗憾,886本次不能参与运算,以后就可以了)
*/
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int count=0;//用于计算数字总和
// "a:"便是使用了 lable 标签,用于给循环取名字,方便后面使用break 来进行中断
a:for(int i=1;i<=10;i++) {
System.out.println("请输入第"+i+"个数字");
int num=input.nextInt();
if(num==886) {
System.out.println("很遗憾,886本次不能参与运算,以后就可以了");
break a;
}
count+=num;
}
System.out.println("输出总和为:"+count);
}
}
关于lable 的面试题
public class Test05{
public static void main(String[] args){
/**
知识点:lable的面试题
描述下列代码第几行报错? 答案:不会报错
原因: http: 等于 直接给下面的循环取了名字,由于 //在代码中代表注释,因而“//www.baidu.com” 会被解释器认为是注释不会编译,所以 “http://www.baidu.com” 相当于是一个lable标签+ 一句注释,所以http: 相当于就是给循环取了个名字,因此不会报错
*/
http://www.baidu.com
for(int i = 0;i<5;i++){
System.out.println("用良心做教育");
}
}
}
项目 打印万年历
源码如下:
import java.util.Scanner;
public class Test07{
public static void main(String[] args){
/**
知识点:万年历
4.万年历:输入一个年份,再输入一个月份,
把那个月的日历打印出来
(1900年1月1日是星期一)。
*/
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:"); //2021
int year = scan.nextInt();
System.out.println("请输入月:"); //7
int month = scan.nextInt();
//1.计算1900~输入年的总天数
int allDayOfYear = 0;
for(int i = 1900;i<year;i++){
if(i%4==0&&i%100!=0 || i%400==0){
allDayOfYear+=366;
}else{
allDayOfYear += 365;
}
}
//2.计算1~输入月的总天数
int allDayOfMonth = 0;
for(int i = 1;i<month;i++){
switch(i){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
allDayOfMonth+=31;
break;
case 4:case 6:case 9:case 11:
allDayOfMonth+=30;
break;
case 2:
if(year%4==0&&year%100!=0 || year%400==0){
allDayOfMonth+=29;
}else{
allDayOfMonth+=28;
}
break;
}
}
//3.年的总天数 + 月的总天数
int allDay = allDayOfYear + allDayOfMonth+1;
//4.获取星期
int week = allDay%7;
System.out.println(week);
//5.获取当月天数
int day = 0;
switch(month){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
day=31;
break;
case 4:case 6:case 9:case 11:
day=30;
break;
case 2:
if(year%4==0&&year%100!=0 || year%400==0){
day=29;
}else{
day=28;
}
break;
}
//6.打印日历
System.out.println(" --- " + year + "年 - " + month + "月 ---");
System.out.println("一\t二\t三\t四\t五\t六\t日");
//打印空格
int num = 0;
for(int i = 1;i<week;i++){
System.out.print("\t");
num++;
}
//打印日期
for(int i = 1;i<=day;i++){
System.out.print(i + "\t");
num++;
if(num%7==0){
System.out.println();
}
}
}
}
运行结果图
总结
提示:今天的任务总结完毕,属于基础知识,其中重点就是对for 循环嵌套的理解,以后在项目中会经常使用到。