jh_01_回顾与作业点评.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_01_回顾与作业点评 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午2:01:50 9 * @version 10 */ 11 12 public class jh_01_回顾与作业点评 { 13 public static void main(String[] args) { 14 // int val = 12345; 15 // int r_digit; 16 // int i = 1; 17 18 /* 19 * 1 : 有5个输出语句,所以用循环。 20 * 2:写个循环5次的循环。 21 * 3:写个输出语句。具体输出什么东东 22 * 4:print(val % 10)==这样值是固定的。 23 * 5:想变就改变val的值。 24 */ 25 26 // System.out.print(val+"反转后的整数是:"); 27 //54321 28 // System.out.print(val%10); 29 // System.out.print(val/10%10); 30 // System.out.print(val/100%10); 31 // System.out.print(val/1000%10); 32 // System.out.print(val/10000); 33 34 35 36 // System.out.print("\n"+val+"反转后的整数是:"); 37 // while (i <= 5) { 38 // System.out.print(val % 10); 39 // val /= 10; 40 // i++; 41 // } 42 43 // System.out.print("\n"+val+"反转后的整数是:"); 44 // for (i = 1; i <= 5; i++) { 45 // r_digit=val%10;//12345 % 10 = 5 46 // val /= 10;//12345/10 = 1234 47 // System.out.print(r_digit); 48 // } 49 50 51 52 53 54 55 56 } 57 }
jh_02_为什么使用for循环.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_02_为什么使用for循环 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午2:16:32 9 * @version 10 */ 11 public class jh_02_为什么使用for循环 { 12 13 public static void main(String[] args) { 14 15 16 // 1.打印10遍"小姐姐,约吗?". 17 18 //初始值 19 // while (循环条件) { 20 // 循环体 21 // 改变循环条件 22 // } 23 24 // for (初始值; 循环条件; 改变循环条件) { 25 // 循环体 26 // } 27 28 29 //while循环 30 // int i = 1; 31 // while (i <= 10) { 32 // System.out.println("小姐姐,约吗?"); 33 // i ++; 34 // } 35 36 // for循环 37 // for (int i = 1; i <=10 ; i++) { 38 // System.out.println("小姐姐,约吗?"); 39 // } 40 41 42 // 2.分别用while循环与for循环打印 10 ————> 1 43 44 //while 45 System.out.print("[while循环打印:10 ——> 1]:\t"); 46 int i = 10; 47 while (i >= 1) { 48 System.out.print("<"+i+">"); 49 i --; 50 } 51 //for 52 System.out.print("\n[for循环打印: 10 ——> 1]:\t"); 53 for (int j = 10; j >= 1; j--) { 54 System.out.print("<"+j+">"); 55 } 56 57 58 59 60 } 61 }
知识点总结:
while循环和for循环语法结构的区别
jh_03_如何使用for循环3_1.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 import java.util.Scanner; 4 5 /** 6 * 项目名称:Java_06_循环结构(二) 7 * 类名称:jh_03_如何使用for循环3_1 8 * 类描述: 9 * 创建人:Beginner 10 * 创建时间:2019年7月9日 下午2:31:58 11 * @version 12 */ 13 public class jh_03_如何使用for循环3_1 { 14 /* 15 * 循环输入某同学的S1结业考试的5门成绩 16 * 17 * 18 * 1:要输入名字。键盘录入 19 * 2:要输入5门成绩用循环。 20 * 3:要求平均分,所以要求总分 21 * 5:用累加求和。+=分数 22 * 6:根据总分求平均分。 23 * 7:输出结果 24 */ 25 public static void main(String[] args) { 26 //1.创建键盘录入对象 27 Scanner sc = new Scanner(System.in); 28 //2.声明两个整型变量——score(分数),sum(存储成绩和), 29 int score,sum = 0; 30 31 System.out.println("请输入姓名:"); 32 String name = sc.next(); 33 //写一个循环5次的循环 34 for (int i = 1; i <= 5; i++) { 35 System.out.print("请输入成绩:第"+i+"门成绩:"); 36 score = sc.nextInt(); 37 sum += score; 38 39 } 40 41 double avgScore = sum/5; 42 43 System.out.println(name+"的5门成绩的平均分为:"+avgScore); 44 sc.close(); 45 } 46 }
jh_04_如何使用for循环3_3.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 import java.util.Scanner; 4 5 /** 6 * 项目名称:Java_06_循环结构(二) 7 * 类名称:jh_04_如何使用for循环3_3 8 * 类描述: 9 * 创建人:Beginner 10 * 创建时间:2019年7月9日 下午4:08:28 11 * @version 12 */ 13 public class jh_04_如何使用for循环3_3 { 14 /* 15 * 16 * 17 */ 18 public static void main(String[] args) { 19 //1.创建键盘录入对象 20 Scanner sc = new Scanner(System.in); 21 22 /*初级版*/ 23 // for (int i = 0,j = 6; i <= 6; i ++,j --) { 24 // System.out.println(i + " + " + j + " = " + (i + j)); 25 // 26 // } 27 28 29 30 31 32 33 /*完整版*/ 34 int num; 35 do { 36 37 System.out.print("请输入一个值:"); 38 num = sc.nextInt(); 39 if (num != 0) { 40 System.out.println("根据这个值可以输出以下加法表:"); 41 for (int i = 0; i <= num ; i ++) { 42 System.out.println(i + " + "+(num - i) + " = " + num); 43 44 } 45 } 46 47 }while (num != 0); 48 49 System.out.println("程序结束!!!"); 50 51 52 53 sc.close(); 54 } 55 }
jh_05_for循环常见问题4_1.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_05_for循环常见问题4_1 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午3:12:40 9 * @version 10 */ 11 public class jh_05_for循环常见问题4_1 { 12 public static void main(String[] args) { 13 14 // for (int i = 1; i <= 10 ; i++) { 15 // System.out.println(i); 16 // } 17 18 19 20 //1.死循环 (声明变量、循环条件、 改变循环条件都不填!!!) 21 // for (; ;) { 22 // 23 // } 24 25 26 27 } 28 }
知识点总结:
1.编译错误:变量 i 没有初始化
2.表达式1省略,循环变量赋初始值在for语句之前由赋值语句完成
3.分号 " ; " 不能省略
4.编译正确,但是缺少循环条件,造成死循环
5.编译通过,但是循环变量的值无变化,造成死循环
6.省略表达式3,在循环体内应设法改变循环变量的值以结束循环
7.表达式全省略,无条件判断,循环变量无改变,应在循环体内设法结束循环;否则会造成死循环
jh_06_现场编程.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_06_现场编程 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午4:08:21 9 * @version 10 */ 11 public class jh_06_现场编程 { 12 public static void main(String[] args) { 13 //1.求1~100之间不能被3整除的数之和 14 int sum = 0; 15 for (int i = 1; i <= 100; i++) { 16 //不能被3整除的数之和 17 if (i % 3 == 0) { 18 sum += i; 19 } 20 21 } 22 System.out.println(sum); 23 24 //2.计算100以内的奇数之和 25 26 for (int i = 1; i <= 100; i+=2) { 27 sum += i; 28 } 29 System.out.println(sum); 30 31 32 33 } 34 }
jh_07_计算100以内奇数之和.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_07_计算100以内奇数之和 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午4:08:16 9 * @version 10 */ 11 public class jh_07_计算100以内奇数之和 { 12 public static void main(String[] args) { 13 int sum = 0; 14 for (int i = 1; i <= 100; i += 2) { 15 sum += i; 16 17 } 18 System.out.println(sum); 19 // int sum = 0; 20 // for (int i = 1; i <= 100; i++) { 21 // if(i % 2 != 0) { 22 // sum += i; 23 // } 24 // } 25 // System.out.println(sum); 26 } 27 }
jh_08_学员操作_计算顾客比例2_1.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 import java.util.Scanner; 4 5 /** 6 * 项目名称:Java_06_循环结构(二) 7 * 类名称:jh_08_学员操作_计算顾客比例2_1 8 * 类描述: 9 * 创建人:Beginner 10 * 创建时间:2019年7月9日 下午3:25:12 11 * @version 12 */ 13 public class jh_08_学员操作_计算顾客比例2_1 { 14 public static void main(String[] args) { 15 Scanner sc = new Scanner(System.in); 16 int count = 0; 17 18 for (int i = 0; i <=10; i++) { 19 System.out.println("请输入第"+i+"个会员的年龄:"); 20 //接收年龄 21 int age = sc.nextInt(); 22 //判断有没有大于30 23 if (age >= 30) { 24 count++; 25 } 26 } 27 System.out.println("顾客中年龄大于30岁的顾客所占比例:"+count/10.0 * 100 +"%"); 28 29 30 sc.close(); 31 32 } 33 }
jh_09_为什么需要break语句.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 /** 4 * 项目名称:Java_06_循环结构(二) 5 * 类名称:jh_09_为什么需要break语句 6 * 类描述: 7 * 创建人:Beginner 8 * 创建时间:2019年7月9日 下午3:29:48 9 * @version 10 */ 11 public class jh_09_为什么需要break语句 { 12 public static void main(String[] args) { 13 14 for (int i = 1; i <= 10; i++) { 15 System.out.println("跑第"+i+"圈"); 16 if (i == 8) { 17 //当i == 8时,用break语句跳出for循环,不再执行此后循环 18 break; 19 20 } 21 } 22 23 } 24 }
jh_10_为什么需要continue语句.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 package lesson; 2 3 import java.util.Scanner; 4 5 /** 6 * 项目名称:Java_06_循环结构(二) 7 * 类名称:jh_10_为什么需要continue语句 8 * 类描述: 9 * 创建人:Beginner 10 * 创建时间:2019年7月9日 下午4:08:41 11 * @version 12 */ 13 public class jh_10_为什么需要continue语句 { 14 public static void main(String[] args) { 15 16 for (int i = 0; i <= 10; i++) { 17 System.out.println("跑第"+i+"圈"); 18 if (i == 8) { 19 continue; 20 21 } 22 } 23 24 25 Scanner sc = new Scanner(System.in); 26 int count = 0; 27 28 for (int i = 0; i <=10; i++) { 29 System.out.println("请输入第"+i+"个会员的年龄:"); 30 //接收年龄 31 int age = sc.nextInt(); 32 //判断有没有大于30 33 if (age < 30) { 34 //当age < 30 ,遇到continue语句跳过count++操作直接进入下一次循环 35 continue; 36 } 37 count++; 38 } 39 System.out.println("顾客中年龄大于30岁的顾客所占比例:"+count/10.0 * 100 +"%"); 40 41 sc.close(); 42 } 43 }