zoukankan      html  css  js  c++  java
  • 6Java控制结构

    顺序结构

    • 从上到下依次执行,中间没有判断和跳转
    • 比如Java中定义变量中的前向应用,先定义,在使用

    分支结构(if, else, switch)

    • 单分支 if
    // if 的快速入门
    import java.util.Scanner;
    class if01{
    
    	public static void main(String[] args) {
    		
    		// 编写一个程序,可以输入人的年龄,如果该同志年龄大于18
    		// 则输出“年龄大于18,对自己的行为负责”
    		// 思路分析
    		// 1.定义一个变量,接受输入的年龄 Scanner
    		// 2.保存在一个变量中 int age
    		// 3.使用if判断
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入年龄:");
    		int age = myScanner.nextInt();
    		if(age > 18){
    			System.out.println("你的年龄大于18,对自己的行为负责");
    		}
    
    		System.out.println("程序继续.....");
    
    	}
    }
    
    • 双分支 if... else....
    // if-else 的快速入门
    import java.util.Scanner;
    class if02{
    
    	public static void main(String[] args) {
    		
    		// 编写一个程序,可以输入人的年龄,如果该同志年龄大于18
    		// 则输出“年龄大于18,对自己的行为负责”,否则输出“你的年龄不大,这次放过你”
    		// 思路分析
    		// 1.定义一个变量,接受输入的年龄 Scanner
    		// 2.保存在一个变量中 int age
    		// 3.使用if-else判断
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入年龄:");
    		int age = myScanner.nextInt();
    		if (age > 18){
    			System.out.println("你的年龄大于18,对自己的行为负责");
    		}
    		else {
    			System.out.println("你的年龄不大,这次放过你");
    		}
    
    		System.out.println("程序继续.....");
    
    	}
    }
    
    • 练习1
    // 编写程序,声明 2 个 double 型变量并赋值。判断第一个数大于 10.0,且第 2 个数小于 20.0,打印两数之和。
    // 想法 声明两个变量, 这个是一个单分支
    
    class IfEx01{
    
    	public static void main(String[] args) {
    		
    		double num1 = 12.0;
    		double num2 = 10.0;
    
    		if(num1 > 10 && num2 < 20){
    			System.out.println("两数之和:" + (num1 + num2));
    		}
    
    		System.out.println("程序继续....")
    
    	}
    }
    
    • 练习02
    //【课后自己练】定义两个变量 int,判断二者的和,是否能被 3 又能被 5 整除,打印提示信息,
    //不成立就打印另外一种情况
    public class IfEx02{
    	public static void main(String[] args) {
    		int num1 = 10;
    		int num2 = 5;
    		int sum = num1 + num2;
    
    		if (sum%3 ==0 && sum%5== 0){
    			System.out.println("两数之和可以被3和5整除");
    		}
    		else{
    			System.out.println("两个数不能被3和5整除");
    		}
    		
    
    	}
    }
    
    • 练习03
    //判断一个年份是否是闰年,闰年的条件是符合下面二者之一:
    //(1)年份能被 4 整除,但不能被 100 整除;(2)能被 400 整除
    import java.util.Scanner;
    class IfEx03{
    
    	public static void main(String[] args) {
    		// 判断年份
    		// if else
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入年份:");
    		int year = myScanner.nextInt();
    
    		if ((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0)){
    			System.out.println(year + "是闰年");
    		}
    		else{
    			System.out.println(year + "不是闰年,是平年");
    		}
    
    	}
    }
    
    • 多分支 if... else if ... else
    • 多分支可以没有else,如果所有的条件表达式都不成立,则一个执行入口都没有
    • 如果所有的表达式都不成立,则默认执行else
    // if -else if.. else
    // 
    import java.util.Scanner;
    
    public class If03{
    
    	public static void main(String[] args) {
    		/*
    		信用分为 100 分时,输出 信用极好; 
    		2) 信用分为(80,99]时,输出 信用优秀;
    		3) 信用分为[60,80]时,输出 信用一般; 
    		4) 其它情况 ,输出 信用 不及格 
    		5) 请从键盘输入保国的芝麻信用分,并加以判断
    		 */
    		// 接受用户输入
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入信用分:");
    		int creditGrade = myScanner.nextInt();
    		// 先对输入的信用分,进行一个范围的判断,否则提示输入错误
    		if (creditGrade <= 100) {
    			// 因为是4种情况,用多分支
    			if (creditGrade == 100){
    				System.out.println("信用极好");
    			}
    			else if (creditGrade > 80){
    				System.out.println("信用优秀");
    			} 
    			else if (creditGrade >= 60){
    				System.out.println("信用一般");
    			}
    			else{
    				System.out.println("信用不及格");
    			}
    		}
    		else{
    			System.out.println("信用分需要在1-100,请重新输入");
    		}
    		
    
    		
    	}
    }
    
    • 嵌套分支,嵌套不要超过三层
    //参加歌手比赛,如果初赛成绩大于 8.0 进入决赛,否则提示淘汰。
    //并且根据性别提示进入男子组或女子组。【可以 让学员先练习下】, 
    //输入成绩和性别,进行判断和输出信息。[NestedIf.java]
    
    import java.util.Scanner;
    public class NestedIf{
    	public static void main(String[] args) {
    		
    		// 输入成绩和性别
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入成绩");
    		double score = myScanner.nextDouble();
    		
    
    		// 判断初赛成绩
    		if (score > 8.0){
    
    			System.out.println("请输入性别:");
    			String string = myScanner.next();
    			char gender = string.charAt(0);
    			if (gender == '男'){
    				System.out.println("你已经进入男子组");
    			}
    			else if (gender == '女'){
    				System.out.println("你进入的是女子组");
    			}
    			else{
    				System.out.println("你的性别输入错误,请重新输入");
    			}
    		}
    		else{
    			System.out.println("sorry, 你已被淘汰");
    		}
    	}
    }
    
    • 练习
    // 出票系统,根据淡旺季的月份和年龄,打印票价
    // 需求: 
    // 接受月份和年龄
    // 进行判断
    
    import java.util.Scanner;
    
    class Ticket{
    
    	public static void main(String[] args) {
    		
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入月份:");
    		int month = myScanner.nextInt();
    		System.out.println("请输入年龄:");
    		int age = myScanner.nextInt();
    		double ticket = 60;
    
    		if (month >= 4 && month <=10){
    			if (age > 60){
    				System.out.println("老人,票价:" + ticket/3);
    			}
    			else if(age >= 18 && age <= 60) {
    				System.out.println("成年人,票价:" + ticket);
    			}
    			else{
    				System.out.println("儿童,半价票,票价:" + ticket/2);
    			}
    
    		}
    		else{
    			if (age >= 18 && age <= 60){
    				System.out.println("成人票价:" + 40);
    			}
    			else{
    				System.out.println("其他票价:" + 20);
    			}
    
    		}
    
    	}
    }
    

    switch

    • 没有break,就会进入到第二个case中
    import java.util.Scanner;
    //请编写一个程序,该程序可以接收一个字符,
    //比如:a,b,c,d,e,f,g a 表示星期一,b 表示星期二
    public class Switch01{
    
    	public static void main(String[] args) {
    		/*
    		思路分析:
    		1. 接受一个字符
    		2. switch判断
    		 */
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入字符:(a, b, c, d, e开头)");
    		char charter = myScanner.next().charAt(0);
    
    		switch(charter) {
    			case 'a': 
    				System.out.println("星期一");
    				System.out.println("多写一句");
    				break;
    			case 'b':
    				System.out.println("星期二");
    				break;
    			case 'c':
    				System.out.println("星期三");
    				break;
    			case 'd':
    				System.out.println("星期四");
    				break;
    			case 'e':
    				System.out.println("星期五");
    				break;
    			case 'f':
    				System.out.println("星期六");
    				break;
    			case 'g':
    				System.out.println("星期天");
    				break;
    			default:
    				System.out.println("你输入的字符不正确,请重新输入");
    		}
    
                    System.put.println("推出了switch");
    
    	}
    }
    
    • 细节
    public class SwitchDetail{
    
    	public static void main(String[] args) {
    		
    		//  细节1
    		//  表达式数据类型,应该和case后的常量类型一致
    		//  或者饿死可以自动转换的类型,比如字符,而常量是 int
    		//  细节2,switch(表达式),表达式的返回值必须是(byte,short, int, char, enum(), String)
    		//  
    		//  细节3
    		//  case子句中的值必须是常量或者常量表达式,二不能是变量;
    		//  
    		//  细节4,default是可选的
    		//  
    		//  细节5,如果没有break,程序会顺序执行到switch结尾
    		char c = 'a';
    		char d = 'c';
    		// double d = 12.9;
    
    		switch(c){
    			case 'a':
    				System.out.println("ok1");
    				// break;
    			case 'c':
    				System.out.println("ok2");
    				break;
    			// default:
    			// 	System.out.println("ok3");
    
    		}
    	}
    }
    
    • 练习
    //1) 使用 switch 把小写类型的 char 型转为大写(键盘输入)。只转换 a, b, c, d, e. 其它的输出 "other"。 
    //2) 对学生成绩大于 60 分的,输出"合格"。低于 60 分的,输出"不合格"。(注:输入的成绩不能大于 100), 提示 成绩/60 
    //3) 根据用于指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 [课堂练习, 提示 使 用穿透
    
    
    import java.util.Scanner;
    
    public class SwitchEx{
    
    	public static void main(String[] args) {
    		
    		Scanner myScanner = new Scanner(System.in);
    		// 1使用 switch 把小写类型的 char 型转为大写(键盘输入)。只转换 a, b, c, d, e. 其它的输出 "other"。
    		System.out.println("请输入字符:");
    		char chacter = myScanner.next().charAt(0);
    
    		switch (chacter){
    			case 'a':
    				System.out.println("A");
    				break;
    			case 'b':
    				System.out.println("B");
    				break;
    			case 'c':
    				System.out.println("C");
    				break;
    			case 'd':
    				System.out.println("D");
    				break;
    			case 'e':
    				System.out.println("E");
    				break;
    			default:
    				System.out.println("other");
    
    		}
    		// 2) 对学生成绩大于 60 分的,输出"合格"。低于 60 分的,输出"不合格"。(注:输入的成绩不能大于 100), 提示 成绩/60 
    		System.out.println("请输入成绩:");
    		double score = myScanner.nextDouble();
    
    		switch ((int)(score / 60)){
    			case 1:
    				System.out.println("合格");
    				break;
    			case 0:
    				System.out.println("不合格");
    				break;
    			default:
    				System.out.println("输入有误");
    		}
    		
    		//3) 根据用于指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 [课堂练习, 提示 使 用穿透
    		System.out.println("请输入月份:");
    
    		int month = myScanner.nextInt();
    
    		switch(month / 3){
    			case 0:
    				System.out.println("冬季");
    				break;
    			case 1:
    				System.out.println("春季");
    				break;
    			case 2:
    				System.out.println("秋季");
    				break;
    			case 3:
    				System.out.println("冬季");
    				break;
    
    		}
    
    	}
    }
    
    • switch和if的比较
    • 如果判断的具体数值不多,而且符合byte,short, int, char, enum, String,虽然两个语句可以用,建议使用switch语句
    • 其他情况,对区间判断,if判断结果更广

    循环结构(for, while, do while, 多重循环)

    • for 循环控制
    public class For01{
    	public static void main(String[] args) {
    		
    		// // 打印10万句,你好,韩顺平教育
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// System.out.println("你好,韩顺平教育");
    		// for 循环控制
    		for(int i = 1; i <= 80; i++){
    			System.out.println("你好,韩顺平教育" + i);
    		} 
    
    	}
    }
    
    • For 细节
    // for 循环控制的细节
    public class ForDetail{
    
    	public static void main(String[] args) {
    		//1) 循环条件是返回一个布尔值的表达式 
    		//2) for(;循环判断条件;) 中的初始化和变量迭代可以写到其它地方,但是两边的分号不能省略。 
    		//3) 循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,循环变量迭代也可以有多条变量迭代 语句,中间用逗号隔开。 
    		//4) 使用内存分析法,老师分析输出下面代码输出什么?
    		//
    		//
    		//2
    		// int i = 1;
    		// for(; i <= 10; ){
    		// 	System.out.println("你好,韩顺平教育");
    		// 	i++;
    		// }
    
    		// System.out.println("i=" + i);
    
    		// 补充,无限循环
    		// int j = 1;
    		// for(;;){
    		// 	System.out.println("你真是很厉害:" + j);
    		// 	j++;
    		// }
    		// 
    		// 3.循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开
    		int count = 3;
    
    		for(int i  = 0, j = 0; i <= count; i++, j+=2){
    			System.out.println("i = " + i + " j = " + j);
    			System.out.println("坚持就是胜利");
    		}
    
    	}
    }
    
    • for 练习1
    • 不知道怎么写代码?掌握重要的编程思想,化繁为简,先死后活
    • 两个编程思想
    • 1.化繁为简,将复杂的需求拆解成简单的需求,逐步完成
    • 2.先死后活,先考虑固定的值,然后转成灵活变化的值
    public class ForEx01{
    
    	public static void main(String[] args) {
    		
    		// 1) 打印 1~100 之间所有是 9 的倍数的整数,统计个数 及 总和.[化繁为简,先死后活]
    		//
    		//化繁为简
    		// 1.完成输出1-100的值]
    		// 2.在输出的过程中,进行过滤,只输出9的倍数
    		// 3.统计个数,定义一个变量,int count=0, 当条件满足时 count++;
    		// 4.总和,定义一个变量,int sum = 0; 当条件满足时累计求和
    		// 
    		// 
    		// 先死后活
    		// 1.为了更好的需求,我们把范围开始的值,结束的值设置为变量
    		// 2.还可以更近一步, 9倍数也做成变量 Int t = 9;
    		int count = 0;
    		int sum = 0;
    		int start = 10;
    		int end = 200;
    		int t = 9;
    		for (int i = start; i <= end; i++){
    			if(i % t == 0){
    				System.out.println("i = " + i);
    				count++;
    				sum += i;
    
    			}
    			
    		}
    		System.out.println("count = " + count);
    
    		// 下面是自己写的,虽然代码是正确的,但是没有思考过程
    		// int count = 0;
    		// int sum = 0; 
    		// for(int i = 1; i <= 100; i++){
    		// 	if (i % 9 == 0){
    		// 		count++;
    		// 		sum += i;
    		// 	}
    		// }
    		// System.out.println("count = " + count + " sum = " + sum);
    
    
    	}
    }
    
    • 练习2
    public class ForEx02{
    	public static void main(String[] args) {
    		
    		// 化繁为简
    		// 1.先输出 0 - 5
    		// 2.后面的+是 5 - i
    		// 
    		// 先死后活
    		// 1. 5替换为n
    		
    		int n = 10;
    
    		for(int i=0; i <= n; i++){
    			System.out.println(i + " + " + (n-i) + " = " + n);
    		}
    		// for(int i=0, j=5; i <= 5; i++, j--){
    		// 	System.out.println(i + " + " + j + " = " + (i + j));
    		// }
    	}
    }
    

    while

    // while 循环的案例
    // 
    public class While01{
    	public static void main(String[] args) {
    		
    		int i = 1;
    		while(i <= 10) {
    			System.out.println("你好,韩顺平教育" + i);
    			i++;  // 循环变量迭代
    		}
    
    		System.out.println("退出while, 继续执行....");
    	}
    }
    
    • while 注意细节
    1. 循环条件是返回一个布尔值的表达式
    2. while 循环是先判断再执行语句
    • while 练习
    // 1) 打印 1—100 之间所有能被 3 整除的数 [使用 while, 老师评讲 ]
    // 
    public class WhilleEx01{
    
    	public static void main(String[] args) {
    		
    		// 化繁为简,先死后活
    		// int i = 1;
    
    		// while(i <= 100){
    		// 	if(i % 3 == 0)
    		// 		System.out.println("i=" + i);
    		// 	i++;
    		// }
    		// 
    		// 
    		// 2) 打印 40—200 之间所有的偶数 [使用 while, 课后练习]
    		int i = 40;
    
    		while (i <= 200){
    
    			if(i % 2 == 0){
    				System.out.println("i=" + i);
    			}
    			i++;
    		}
    	}
    }
    
    
    • do...while 循环控制
    • do while 细节
      1)循环条件是一个返回布尔值的表达式
      2)do..while 循环是先执行,然后在判断,至少执行一次
    • do while 练习
    import java.util.Scanner;
    
    public class DoWhileEx01{
    
    	public static void main(String[] args) {
    		
    		// 打印1-100
    		// 
    		// System.out.println("打印100");
    
    		// int i = 1;
    		// do{
    		// 	System.out.println("i=" + i);
    		// 	i++;
    		// } while(i <= 100);
    
    		// System.out.println("do while 结束,程序继续");
    
    
    		// 计算1-100和
    		// int j = 1;
    		// int sum = 0;
    		// do {
    		// 	sum += j;
    		// 	j++;
    		// } while(j <= 100);
    
    		// System.out.println("1-100的和sum=" + sum);
    
    		//统计 1---200 之间能被 5 整除但不能被 3 整除的个数
    		//
    		
    		// int k = 1;
    		// int count = 0;
    
    		// do {
    		// 	if (k % 5 == 0 && k % 3 != 0){
    		// 		count++;
    		// 	}
    		// 	k++;
    		// }while(k <= 200);
    
    		// System.out.println("count = " + count);
    		// 
    		// 如果李三不还钱,则老韩将一直使出五连鞭,直到李三说还钱为止
    		Scanner myScanner = new Scanner(System.in);
    		// char anwser = ' ';
    		do{
    			System.out.println("使出闪电五连鞭");
    			System.out.println("还钱吗?(y/n):");
    
    			char anwser = myScanner.next().charAt(0);
    
    		} while (anwser != 'y');
    
    		System.out.println("李四还钱了");
    	}
    }
    

    多重循环(重点)

    1. 将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while 均可以作为外层循环和内层循环。 【建议一般使用两层,最多不要超过 3 层, 否则,代码的可读性很差】
    2. 实质上,嵌套循环就是把内层循环当成外层循环的循环体。当只有内层循环的循环条件为 false 时,才会完全跳出内 层循环,才可结束外层的当次循环,开始下一次的循环[听不懂,走案例]。
    3. 设外层循环次数为 m 次,内层为 n 次,则内层循环体实际上需要执行 m*n 次
    public class MulFor{
    
    	public static void main(String[] args) {
    		
    		for (int i = 0; i < 2; i++){
    			for (int j = 0; j < 3; j++){
    				System.out.println("i = " + i + " j = " + j);
    			}
    		}
    	}
    }
    
    • 多重循环的练习题
    // 统计 3 个班成绩情况,每个班有 5 名同学,求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]
    import java.util.Scanner;
    
    public class MulForEx01{
    	public static void main(String[] args) {
    		
    		// 统计 3 个班成绩情况,每个班有 5 名同学,求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]
    		// 3 * 5
    		// 思路,先求出一个班级的学生的平均分
    		// 然后在求出三个班级的平均分
    		// 化繁为简,先死后活
    		double stuScore;
    		double classScore;
    		double allScore;
    		Scanner myScanner = new Scanner(System.in);
    		double avgAllScore = 0;
    		int classNum = 3; // 班级数量
    		int stuNum = 5; // 学生数量
    		int passNum = 0; // 及格人数
    
    		for (int i = 1; i <= classNum; i++){
    			double avgClassScore = 0;
    
    			for (int j = 1; j <= stuNum; j++){
    				System.out.println("请输出第" + i + "班级的第" + j + "学生的成绩");
    				stuScore = myScanner.nextDouble();
    				if (stuScore >= 60){
    					passNum++;
    				}
    				avgClassScore += stuScore;
    				avgAllScore += stuScore;
    			}
    
    			System.out.println("第" + i + "班级的平均分:" + avgClassScore/stuNum);
    		}
    		System.out.println("所有及格的人数:" + passNum);
    		System.out.println("所有的班级平均分:" + avgAllScore/(stuNum * classNum));
    	}
    }
    

    -- 九九乘法表

    // 统计 3 个班成绩情况,每个班有 5 名同学,求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]
    import java.util.Scanner;
    
    public class MulForEx01{
    	public static void main(String[] args) {
    		
    		// 打印出九九乘法表[课后题]
    		// 打印 找规律
    		for(int i = 1; i <= 9; i++){
    			for(int j = 1; j <= i; j++){
    				System.out.print(j + " * " + i + " = " + i*j + "    ");
    			}
    			System.out.println();
    		}
    	}
    }
    
    • 打印金字塔,学到颇多,如何有章法的分析问题
    public class Stars{
    
    	public static void main(String[] args) {
    		/*
    		思路分析:
    		化繁为简,先死后活
    
    		1.化繁为简
    		1.先打印矩形
    		*****
    		*****
    		*****
    		*****
    		*****
    		2.打印半个金字塔
    		*
    		**
    		***
    		****
    		*****
    		3.打印整个金字塔
    		*     1  空格  5 - 1
    	   ***    3        5 - 2
    	  *****   5
    	 *******  7
    	********* 9
    		 */
    		
    		for(int j = 1; j <= 5; j++){
    			// 控制每层" "的个数
    			for(int k = 1; k <= 5 - j; k++){
    					System.out.print(" ");
    			}
    			// 控制每层*的个数
    			for(int i = 1; i <= 2*j-1; i++){
    				
    				System.out.print("*");
    			}
    
    			System.out.println();
    		}
    		
    
    	}
    }
    
    • 打印空心金字塔
    public class Stars{
    
    	public static void main(String[] args) {
    		/*
    		思路分析:
    		化繁为简,先死后活
    
    		1.化繁为简
    		1.先打印矩形
    		*****
    		*****
    		*****
    		*****
    		*****
    		2.打印半个金字塔
    		*
    		**
    		***
    		****
    		*****
    		3.打印整个金字塔
    		*     1  空格  5 - 1  
    	   ***    3        5 - 2
    	  *****   5
    	 *******  7
    	********* 9
    	    4. 打印空心金字塔
    	    *     1  空格  5 - 1    空心0
    	   * *    3        5 - 2       1
    	  *   *   5                    3
    	 *     *  7                    5
    	********* 9
    		 */
    		
    		for(int i = 1; i <= 5; i++){
    			// 控制每层" "的个数
    			if(i <= 4){
    				for(int k = 1; k <= 5 - i; k++){
    					System.out.print(" ");
    				}
    				// 控制每层*的个数
    				System.out.print("*");
    				for(int j = 1; j <= 2*i-3; j++){
    					System.out.print(" ");
    				}
    				if (i > 1){
    					System.out.print("*");
    				}
    				
    			}
    			else{
    				System.out.print("*********");
    			}
    			
    
    			System.out.println();
    		}
    		
    
    	}
    }
    
    • 自己写的比较蠢,老师的比较精炼
    public class Stars{
    
    	public static void main(String[] args) {
    		/*
    		思路分析:
    		化繁为简,先死后活
    
    		1.化繁为简
    		1.先打印矩形
    		*****
    		*****
    		*****
    		*****
    		*****
    		2.打印半个金字塔
    		*
    		**
    		***
    		****
    		*****
    		3.打印整个金字塔
    		*     1  空格  5 - 1  
    	   ***    3        5 - 2
    	  *****   5
    	 *******  7
    	********* 9
    	    4. 打印空心金字塔
    	    *     1  空格  5 - 1    空心
    	   * *    3        5 - 2
    	  *   *   5
    	 *     *  7
    	********* 9
    		 */
    		// 先死后活
    		//  int totalLevel = 5;
    		// 
    		int totalLevel = 20;
    		for(int i = 1; i <= totalLevel; i++){
    			// 控制每层" "的个数
    			for(int k = 1; k <= totalLevel - i; k++){
    					System.out.print(" ");
    			}
    			// 控制每层*的个数
    			for(int j = 1; j <= 2*i-1; j++){
    				if(j == 1 || j == 2*i-1 || i == totalLevel){
    					System.out.print("*");
    				}
    				else{
    					System.out.print(" ");
    				}
    			}
    
    			System.out.println();
    		}
    		
    
    	}
    }
    
    • 打印空心菱形,比较笨
    public class Stars{
    
    	public static void main(String[] args) {
    		/*
    		思路分析:
    		化繁为简,先死后活
    
    		1.化繁为简
    		1.先打印矩形
    		*****
    		*****
    		*****
    		*****
    		*****
    		2.打印半个金字塔
    		*
    		**
    		***
    		****
    		*****
    		3.打印整个金字塔
    		*     1  空格  5 - 1  
    	   ***    3        5 - 2
    	  *****   5
    	 *******  7
    	********* 9
    	    4. 打印空心金字塔
    	    *     1  空格  5 - 1    空心
    	   * *    3        5 - 2
    	  *   *   5
    	 *     *  7
    	********* 9
    		 */
    		// 先死后活
    		//  int totalLevel = 5;
    		// 
    		int totalLevel = 5;
    		for(int i = 1; i <= totalLevel; i++){
    			// 控制每层" "的个数
    			for(int k = 1; k <= totalLevel - i; k++){
    				System.out.print(" ");
    				
    			}
    			// 控制每层*的个数
    			for(int j = 1; j <= 2*i-1; j++){
    				if(j == 1 || j == 2*i-1){
    					System.out.print("*");
    				}
    				else{
    					System.out.print(" ");
    				}
    			}
    			
    			
    			System.out.println();
    		}
    
    		// 倒过来打印即可
    		for(int i = 1; i <= totalLevel; i++){
    			// 控制每层" "的个数
    			for(int k = 1; k <= i; k++){
    				System.out.print(" ");
    				
    			}
    			// 控制每层*的个数
    			for(int j = 1; j <= 2*(totalLevel-i)-1; j++){
    				if(j == 1 || j == 2*(totalLevel-i)-1){
    					System.out.print("*");
    				}
    				else{
    					System.out.print(" ");
    				}
    			}
    			
    			
    			System.out.println();
    		}
    
    
    	}
    }
    

    break

    当循环满足条件时,可以终止循环

    
    public class Break01{
    
    	public static void main(String[] args) {
    		
    		for(int i = 0; i < 10; i++){
    			if(i == 3){
    				break;
    			}
    			System.out.println("i = " + i);
    		}
    
    		System.out.println("退出for循环");
    	}
    }
    
    • break细节
    • 可以终止选择的循环
    public class BreakDetail{
    
    	public static void main(String[] args) {
    		
    		label1:
    		for(int j=0; j < 4; j++){
    		abc:
    			for(int i = 0; i < 10; i++){
    				if(i == 2){
    					break label1;
    				}
    				System.out.println("i = " + i);
    			}
    		}
    	}
    
    }
    
    • break练习1
    public class BreakEx01{
    	public static void main(String[] args) {
    		// 1-100 以内的数求和,求出 当和 第一次大于 20 的当前数 【for + break】
    		
    		int sum = 0;
    		// i的作用范围在 for {}
    		// 定义一个变量n, 把当前的i赋值为n
    		int n = 0;
    		for (int i = 1; i <= 100; i++){
    			sum += i;
    			if (sum > 20){
    				System.out.println("和大于20的时候,当前数i=" + i);
    				n = i;
    				break;
    			}
    
    		}
    
    		System.out.println("和大于20的时候,当前数n=" + n);
    	}
    }
    
    • break练习2
    // 实现登录验证,有 3 次机会,
    // 如果用户名为"丁真" ,密码"666"提示登录成功,否则提示还有几次机会,请使用 for+break 完成 
    
    import java.util.Scanner;
    
    public class BreakEx02{
    	public static void main(String[] args) {
    		Scanner myScanner = new Scanner(System.in);
    		int chance = 3;
    		for (int i = 1; i <= 3; i++){
    			System.out.println("请输入用户名:");
    			String name = myScanner.next();
    			System.out.println("请输入密码:");
    			String password = myScanner.next();
    
    			// 字符串比较 equals
    			if (name.equals("丁真") && password.equals("666")){
    				System.out.println("登录成功....");
    				break;
    			}
    			chance--;
    			System.out.println("你还剩" + chance + "次机会");
    		}
    	}
    }
    

    continue

    • 用于结束本次循环,开始下一次的循环
    • continue 语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环 , 这个和前面的标签的 使用的规则一样
    • 快速入门
    public class Continue01{
    	public static void main(String[] args) {
    		// 代码
    		int i = 1;
    		while (i <= 4){
    			i++;
    			if (i == 2){
    				continue;
    			}
    			System.out.println("i = " + i);
    		}
    
    	}
    }
    
    • continue 细节
    public class ContinueDetail{
    
    	public static void main(String[] args) {
    		
    		label1:
    		for(int j = 0; j < 4; j++){
    			label2:
    			for(int i = 0; i < 10; i++){
    				if (i == 2){
    					// 看看分别输出什么值
    					// continue label2; // continue;
    					continue label1;
    				}
    				System.out.println("i = " + i);
    			}
    		}
    	}
    }
    

    return

    • 跳出方法
    
    public class Return01{
    	public static void main(String[] args) {
    		
    		for(int i = 1; i<=5; i++){
    			if(i == 3){
    				System.out.println("教育 " + i);
    				return;
    			}
    			System.out.println("Hello World~");
    		}
    
    		System.out.println("go on..");
    	}
    }
    

    控制结构的作业

    • 作业1
      1.某人有100000元,经过一次路口进行缴费,现金 > 50000时,交 5%
      现金 <= 50000时,每次交1000
      问题,该人可以经过多少次路口
    // 1.某人有100000元,经过一次路口进行缴费,
    // 现金 > 50000时,交 5%
    // 现金 <= 50000时,每次交1000
    // 问题,该人可以经过多少次路口
    public class HomeWork01{
    	public static void main(String[] args) {
    		int count = 0;
    		double moneySum = 100000;
    
    		while (moneySum > 0){
    			if (moneySum > 50000){
    				moneySum -= moneySum * 0.05;
    				count++;
    			}
    			else if (moneySum <= 50000 & moneySum >= 1000){
    				moneySum -= 1000;
    				count++;
    			}
    			else {
    				break;
    			}
    		}
    
    		System.out.println("count的值:" + count);	
    
    	}
    }
    
    • 作业2
      实现判断一个整数,属于哪个范围 大于0, 小于0, 等于0
    // 实现判断一个整数,属于哪个范围 大于0, 小于0, 等于0
    import java.util.Scanner;
    
    public class HomeWork02{
    	public static void main(String[] args) {
    		
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入一个整数:");
    		int num = myScanner.nextInt();
    
    		if (num > 0){
    			System.out.println("这个整数大于0");
    			System.out.println("num 是 " + num);
    		}
    		else if (num < 0) {
    			System.out.println("这个整数小于0");
    			System.out.println("num 是 " + num);
    		}
    		else {
    			System.out.println("这个整数等于0");
    			System.out.println("num 是 " + num);
    		}
    
    	}
    }
    
    • 作业3
      判断一个年份是闰年
    // 判断一个年份是闰年
    import java.util.Scanner;
    
    public class HomeWork03{
    	public static void main(String[] args) {
    		
    		Scanner myScanner = new Scanner(System.in);
    		System.out.println("请输入一个年份:");
    		int year = myScanner.nextInt();
    
    		if(year % 400 == 0 || (year%4==0 && year%100 != 0)){
    			System.out.println("年份" + year + "是闰年");
    		}
    		else{
    			System.out.println("该年份" + year + "是平年");
    		}
    
    	}
    }
    
    • 作业4
      判断一个三位数是否是水仙花数,153 = 111 + 333 + 555
    // 判断一个三位数是否是水仙花数,153 = 1*1*1 + 3*3*3 + 5*5*5
    import java.util.Scanner;
    
    public class HomeWork04{
    	public static void main(String[] args) {
    		System.out.println("请输入一个三位数:");
    		Scanner myScanner = new Scanner(System.in);
    		int num = myScanner.nextInt();
    
    		int singal_digit = num % 10;
    		int ten_digits = (num % 100) / 10;
    		int hundreds = num / 100;
    
    		int cubeNum = singal_digit*singal_digit*singal_digit + ten_digits*ten_digits*ten_digits + hundreds*hundreds*hundreds;
    
    		if (num == cubeNum){
    			System.out.println(num + "该数字是水仙花数");
    		}
    		else{
    			System.out.println(num + "该数字不是水仙花数");
    		}
    
    
    
    	}
    }
    
    • 作业5
      输出1-100不能被5整除的数,每五个一行
    // 输出1-100不能被5整除的数,每五个一行
    public class HomeWork05{
    	public static void main(String[] args) {
    		
    		for (int i = 1; i <= 100; i++){
    			if (i % 5==0){
    				System.out.println();
    				continue;
    			}
    			System.out.print(i + " ");
    		}
    	}
    }
    

    更优的做法

    // 输出1-100不能被5整除的数,每五个一行
    public class HomeWork05{
    	public static void main(String[] args) {
    		
    		// 哨兵
    		int count =  0;
    		for (int i = 1; i <= 100; i++){
    			count++;
    			if (i % 5 != 0){
    				System.out.print(i + "\t");
    			}
    
    			if (count % 5 == 0){
    				System.out.println();
    			}
    		}
    	}
    }
    
    • 作业6
      输出小写的a-z,输出大写的Z-A
    // 输出小写的a-z,输出大写的Z-A
    public class HomeWork06{
    	public static void main(String[] args) {
    		
    		
    		for (char i = 'a'; i <= 'z'; i++){
    			System.out.print(i + " ");
    		}
    		System.out.println();
    		for (char i = 'Z'; i >= 'A'; i--){
    			System.out.print(i + " ");
    		}
    	}
    }
    
    • 作业7
      求1-1/2+1/3-1/4......-1/100
    // 求1-1/2+1/3-1/4......-1/100
    public class HomeWork07{
    	public static void main(String[] args) {
    		double sum = 0;
    
    		for(int i = 1; i <= 100; i++){
    			if (i % 2 != 0){
                                    // 主要要把分子写成1.0
    				sum += 1.0/i;
    			}
    			else{
    				sum -= 1.0/i;
    			}
    		}
    		System.out.println("sum = " + sum);
    	}
    }
    
    • 作业8
      求 1 + (1+2) + (1+2+3) + (1+2+3.....+100)
    // 求 1 + (1+2) + (1+2+3) + (1+2+3.....+100)
    public class HomeWork08{
    	public static void main(String[] args) {
    		int sum = 0;
    
    		for(int i=1; i<=100; i++){
    			int branchSum = 0;
    			for (int j = 1; j <= i; j++){
    				branchSum += j;
    			}
    			sum += branchSum;
    		}
    
    		System.out.println("sum = " + sum);
    	}
    }
    
  • 相关阅读:
    石家庄地铁线路查询系统(补)
    构建之法阅读笔记03
    构建之法阅读笔记02
    Day 3-3 内置方法
    Day3-2 函数之递归
    Day3-1 函数
    Day2 列表,元组,字典,集合
    Day1 基础知识
    Day1. Python基础知识
    iptables防火墙配置
  • 原文地址:https://www.cnblogs.com/jly1/p/15643643.html
Copyright © 2011-2022 走看看