> 文章列表 > day06_for_关键词

day06_for_关键词

day06_for_关键词

今日内容

零、 复习昨日
一、作业
二、循环
三、控制语句关键词
四、总结

零、 复习昨日

见晨考

一、作业

package com.qf.homework;/* --- 天道酬勤 --- @author QiuShiju* @desc*/
public class Demo2 {public static void main(String[] args) {// 水仙花是一个三位数,// 这个数的个位的立方+十位的立方+百位的立方==他本身:// 153   =   27+125+1int num = 153;int ge = num % 10;// System.out.println(ge );int shi = num / 10  % 10;// System.out.println(shi );int bai = num / 100;// System.out.println(bai );if (ge*ge*ge + shi*shi*shi + bai*bai*bai == num) {System.out.println(num+"是水仙花数" );} else {System.out.println(num+"不是水仙花数" );}System.out.println("---------------" );int i = 100;while (i < 1000) {int g = i % 10;int s = i / 10  % 10;int b = i / 100;if (g*g*g + s*s*s + b*b*b == i) {System.out.println(i+"是水仙花数" );}i++;}System.out.println("-------------" );// 输入任何一个数字,倒着输出每一位数字/*  153*  153 % 10 = 3*  153 / 10 % 10 = 5*  153 / 10 /10 % 10 = 1  1453*  1453 % 10 = 3*  1453 / 10 % 10 = 5*  1453 / 10 /10 % 10 = 4*  1453 / 10 / 10 / 10 % 10 = 1;  14567*  14567 % 10 = 7;*  14567 / 10 % 10 = 6;*  14567 / 10 / 10 % 10 = 5;*  14567 / 10 / 10 /10 % 10 = 4;*  14567 / 10 / 10 /10 /10 % 10 = 1;*/int n = 345678;while (n > 0) {int w = n % 10;System.out.println(w );n = n / 10;}System.out.println("----------------" );// 求得一个数是几位数?// 分析:将这个数循环除以10记录运算了几次int x = 1234567;int count = 0;while(x > 0){x = x / 10;count++;}System.out.println(count+"位数" );}
}

二、循环

实现代码重复执行–循环

  • while
  • do-while
  • for

复习

while(布尔表达式) {// 执行语句
}
// 先判断while,如果结果是true就执行循环体
// 执行完后继续返回判断布尔表达式,如果再为true继续执行
// 只有当while后条件判断为false时跳过循环不再执行

while循环又叫 当型循环

2.1 do-while[了解]

语法

do {// 执行内容 
}while(布尔表达式);
/*
1 先执行一次do内的代码
2 然后再执行while后的判断
3 如果结果是true,就继续执行do内的代码
4 直到while后的条件为false时跳过循环
-----
(先斩后奏)
*/

do-while又叫 直到型循环

package com.qf.loop;/* --- 天道酬勤 --- @author QiuShiju* @desc 演示do-while循环*/
public class Demo3 {public static void main(String[] args) {/* do{*     // 内容* }while(条件);*/// 输出1-10int i = 1;do {System.out.println(i );i++;}while (i < 11);// 输出1-100的偶数int j = 1;do{if (j % 2 == 0) {System.out.println(j );}j++;}while (j < 101);}
}

总结: while和do-while异同

  • 一样的是都可以循环
  • 不一样
    • while先判断后执行
    • do-while先执行一次再判断
    • 即使条件不成立,do-while也会执行一次

2.2 for循环[重要]

语法:

for(初始值;条件;迭代){// 循环体
}

day06_for_关键词

练习

package com.qf.loop;/* --- 天道酬勤 --- @author QiuShiju* @desc  for循环*/
public class Demo4 {public static void main(String[] args) {/* for(初始值;条件;迭代){*     // 执行语句* }*/// 输出1-10 ?for(int i = 1 ; i < 11 ; i++) {System.out.println(i );}/* 执行流程:* 1) 执行到for,先执行初始值* 2) 判断表达式,结果是true,向下执行循环体* 3) 执行完再回头执行迭代* 4) 后再判断表达式,结果是true则继续...* 5) 直到判断为false时跳出循环*/// for(;;){//     System.out.println("xxxxxx" );// }// 输出10-1int i;for(i = 10;i > 0;i--) {System.out.println(i );}System.out.println(i );// 输出1-100的偶数for(int j = 1;j < 101;j++) {if (j % 2 == 0) {System.out.println(j );}}for(int j = 2;j < 101;j+=2) {System.out.println(j );}// 输出1-100的和int sum = 0;for(int j = 1;j < 101;j++) {//sum = sum + j;sum += j;}System.out.println("1-100的和"+sum );// 输出1-100的奇数和// 输出1-100的偶数和int oddSum = 0; // 奇数和int evenSum = 0; // 偶数和int sum2 = 0; // 总和for (int j = 1;j < 101;j++) {if (j % 2 == 0) {evenSum += j;} else {oddSum += j;}sum2 += j;}System.out.println("奇数和:" +  oddSum+",偶数和:"+evenSum +",总和:"+ sum2);// 使用for循环完成水仙花数}
}

2.2 for嵌套

package com.qf.loop;/* --- 天道酬勤 --- @author QiuShiju* @desc*/
public class Demo5 {public static void main(String[] args) {/* * * * */// 外面控制行for (int i = 1; i < 6; i++) {// 控制每行中的列for (int j = 1; j < 10; j++) {System.out.print("*");}System.out.println( );}System.out.println("----------------" );for (int i = 1; i < 5; i++) {for (int j = 1; j < 9; j++) {System.out.print("*" );}System.out.println( );}System.out.println("----------" );/*  *  *  * /// 外层循环控制行数for (int i = 1; i < 8; i++) {// 内存循环控制列数for (int j = 1; j <= i; j++) {System.out.print("*" );}System.out.println( );}/* 99乘法口诀表* 1*1=1* 1*2=2 2*2=4* 1*3=3 2*3=6 3*3=9* ....*/for (int i = 1; i < 10; i++) {for (int j = 1; j <= i; j++) {System.out.print(j+" * "+i+" = "+(i * j)+"\\t");}System.out.println( );}System.out.println("-------------" );/*      .....n     2n-1*/for (int i = 1; i < 6; i++) {for (int j = 1; j <= 2*i-1; j++) {System.out.print("*" );}System.out.println( );}   }
}

for循环嵌套的重点: 要理解代码的执行顺序

三、控制语句关键词[熟悉]

在流程控制语句内使用的一些关键词,也可以实现控制流程语句的执行

  • break
  • continue

3.1 break

break 破坏/中断/打断

  • break可以用在switch中
  • break可以用在循环中

使用场景1: 在switchcase中用来重点switch执行

使用场景2: 在循环中使用,用来中断循环,循环不再执行

package com.qf.loop;import java.util.Scanner;/* --- 天道酬勤 --- @author QiuShiju* @desc break关键词*/
public class Demo6 {public static void main(String[] args) {/* break* 中断当前层循环*/for (int i = 1; i < 11; i++) {if (i == 5) {break; // 中断循环}System.out.println(i );}System.out.println("for后" );for (int i = 1; i < 11; i++) {for (int j = 1; j < 11; j++) {if (j == 5) {break;}System.out.print("j = " + j );}System.out.println("i = " + i );System.out.println( );}System.out.println("--------" );// Scanner scanner = new Scanner(System.in);// while(true) {//     System.out.println("请输入年龄:");//     int age = scanner.nextInt( );//     if (age == 886) {//         System.out.println("再见~" );//         break;//     }//     if (age >= 18) {//         System.out.println("可以考驾照");//     } else {//         System.out.println("不可以考");//     }// }/* 次数已知,用for* 次数未知,用while*//* ATM机取钱:* 假设密码是数字123,允许输入3次* 如果输入正确,直接进入,给出提示:欢迎使用天地银行* 如果不正确,继续下一次输入* 如果三次都不对,给出提示: 卡已锁死,请联系客服~*/Scanner scan = new Scanner(System.in);System.out.println("欢迎使用<<天地银行>>" );for (int i = 1; i < 4; i++) {System.out.println("总共3次机会,正在第"+i+"次输入,请输入密码:" );int password = scan.nextInt( );if (password == 123) {System.out.println("欢迎登录~" );// 登录成功,中断循环break;} else {System.out.println("密码错误" );}// 循环3次,都没有执行上方的break,说明3次都密码错误if (i == 3) {System.out.println("已输错3次,卡锁死,联系客服" );}}}
}

3.2 continue

continue 继续,该关键词只能在循环中使用

package com.qf.loop;/* --- 天道酬勤 --- @author QiuShiju* @desc*/
public class Demo7 {public static void main(String[] args) {/* continue 继续* 打断本次循环,继续下次循环*/for (int i = 1; i < 11; i++) {if (i == 6) {continue;}System.out.println(i );}System.out.println("-----------" );// 逢七过for (int i = 1; i < 101; i++) {// 每10个数换行if (i % 10 == 0) {System.out.println( );}// 7的倍数 || 个位是7 || 十位是7if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {System.out.print("过 ");continue;}System.out.print(i+" ");}// 找出1-100以内含7的数}
}

四、总结

1 小技巧: 利用java的除法。除以一个数是取整不保留小数
2 循环就是为了解决代码重复问题while dowhilefor
3 for循环语法,执行流程
4 for循环嵌套执行流程
5 break     打断switch,循环continue  打断循环,继续下次循环

作业

1 重复2遍
2 有时间记笔记
3 背单词
4 

附录

count 技术
loop 循环
odd 奇数
even 偶数
password 密码
continue 继续
for  为了
alt + / 提示代码