Java小白 学习心得 和 笔记总结(第三天)----switch case 和 循环结构

前言

虽然现在自己还是个菜鸟,但是我会努力,会加油,会比别人付出更多,每天认真总结笔记,认真多敲几遍代码!(知行合一)

switch case 和 循环结构

1. switch case

1.1 结构概述

switch ( /* 变量 */ ){

	case 常量选择1:
	//处理方式1;
	break;
	
	case 常量选择2:
	//处理方式2;
	break;

	case 常量选择3;
	//处理方式3;
	break;

	default:
	//最终处理方式;
	break;	
}

执行的流程:
代码运行到 switch case 结构
首先取出 switch 之后小括号中变量保存的数据
匹配 switch 大括号之内的 case 选择,找到对应的常量选择匹配。
如果找到对应的匹配选择,执行对应的处理方式。
如果没有找到匹配的选择,执行 default 里边的处理方式。
break 这个关键字蛀牙用于直接跳出 switch case 运行。

switch case 演示点菜
1. 肉夹馍
2. 凉皮
3. 煎饼果子
4. 炒面
5. 米饭

/*
	switch case 演示点菜
	1. 肉夹馍
	2. 凉皮
	3. 煎饼果子
	4. 炒面
	5. 米饭
*/
	
import java.util.Scanner;	

class Demo1{
	
	public static void main(String[] args){
		
		//这里初始化变量 选择一个int类型的变量
		int choose = 0;
		Scanner sc = new Scanner(System.in);
		
		//展示菜单
		System.out.println("欢迎来到xx餐厅");
		System.out.println("1. 肉夹馍");
		System.out.println("2. 凉皮");
		System.out.println("3. 煎饼果子");
		System.out.println("4. 炒面");
		System.out.println("5. 米饭");
		choose = sc.nextInt();
		
		switch ( choose ) {
			
			case 1:
				System.out.println("肉夹馍  10 RMB");
				break;
			case 2:
				System.out.println("凉皮  6RMB");
				break;
			case 3:
				System.out.println("煎饼果子 5RMB");
				break;
			case 4:
				System.out.println("炒面 15RMB");
				break;
			case 5:
				System.out.println("米饭 20RMB");
				break;
			default:
				System.out.println("不好意思,你选错了");
				break;
		}
	}
	
}

switch- case使用的注意事项

1.在switch-case结构中,有且只能执行case,或者default之后的语句。如果存在case或者default之外的语句,是无法执行的,对于java来说是无效代码。(unceachable code)
2.在switch-case结构中,如果存在case或者defaut选择缺少break操作。代码会继续运行到下一个break!或者大括号(switch-case结尾的大括号)终止switch-case!
3.在switch-case结构中,不允许出现想用的case选项,否则代码无法运行选择,因为所有的case选择是平级的。
4.在switch-case结构中,default可以省略,代码编译不会受到影响,没有语法错误。但是失去switch case自带的一种异常情况的处理方式(就是除了case之外的条件的话无法运行)
AJCG阿里巴巴Java开发规范中,是不推荐省略default的

1.3总结

1.switch case 结构和 if else if 结构类似,两者可以是互通的。
2.if else if 结构中可以处理范围性数据,处理的数据区间更加广泛。
3.switch case处理数据明确性更强,处理针对选择比较合适。

2.循环结构

2.1 while循环

while (/*循环条件 ture or false */){
	//循环体
}

执行流程:
当前程序运行到while循环结构时,首先判断while之后的小括号里边的循环条件是否为ture,如果为ture,执行循环,再回到循环条件判断,知道循环条件为false,终止循环!!!

用switch case程序来做
“一周七天,吃饭睡觉打豆豆“

/*
一周七天
吃饭睡觉打豆豆
*/
class Demo2{
	public static void main(String[] args){
		int i = 1;
		while (i <= 7){
			System.out.println(i + ": 吃饭睡觉打豆豆");
			i += 1;
		}
		System.out.println("i=" + i);
	}
}

用while循环结构
展示 1 ~ 100

/*
while循环结构
	展示数值 1 ~ 100
*/
class Demo3 {
	public static void main(String[] args) {
		int i = 1;

		while (i <= 100) {
			System.out.println("i : " + i);
			i += 1;
		}
	}
}

while 循环结构
展示数值 1 ~ 100 所有数值的和

class Demo8 {
	public static void main(String[] args) {
		int i = 1;
		int sum = 0;
		
		while (i <= 100) {
			sum += i;
			i += 1;		
		}
		
		System.out.println("Sum : " + sum);
		System.out.println((1 + 100) * 50);
	}
}

2.3 do- while循环

do{
	//循环体
}while(/*循环判断条件*/)

执行流程:
当程序执行到do-while循环结构时,首先执行一次循环体(循环条件变更),再来判断while中循环条件判断是否为ture,如果为ture,继续执行,如果为false,终止循环。

使用do-while循环,展示1~100所有的偶数

class Demo4{
	public static void main(String[] args){
		int i = 1;
		do {
			if (i % 2 == 0){
			System.out.println("i=" + i);
			}
			i +=1;
			}while (i <= 100);
	}
}

上边的代码 还可以进一步的优化

class Demo4{
	int j = 2;
	do {
			System.out.println("j = " + j);
			j += 2;
		} while (j <= 100);
}

使用do - while循环
完成 1 ~ 150的和

class Deom5{
	public static void main(String[] args){
	int i = 1;
	int sum = 0;
	do {
    	sum += i;
    	i += 1;
	}while(i <= 150);
	System.out.println("sum=" + sum);
	}
}

使用do - while循环
展示大写字母A ~ Z

class Demo12 {
	public static void main(String[] args) {
		char ch = 'a';
		
		do {
			System.out.println("ch : " + ch);
			ch += 1;
			// ch++; ++ch;
		} while (ch <= 'z');
	}
}

2.4循环和分支的嵌套

/*
使用while循环,完成一个点菜系统
	1. 完成用户的点菜功能
	2. 点菜之后输出总价
	3. 用户指定方式退出
*/
import java.util.Scanner;

class Demo13 {
	public static void main(String[] args) {
		/*
		这里需要一个变量 int类型,作为用户的选择
		变量名 choose
		*/
		int choose = 0;
		/*
		计算得到总价格,int类型,变量名为 total
		*/
		int total = 0;
		/*
		准备一个Scanner类型的变量,用于从键盘上获取用户
		输入的数据
		*/
		Scanner sc = new Scanner(System.in);
		
		System.out.println("欢迎来到xx商店");
		System.out.println("1. 啤酒 8RMB");
		System.out.println("2. 方便面 88RMB");
		System.out.println("3. 辣条 25RMB");
		System.out.println("4. 烟 6RMB");
		System.out.println("5. 可乐 18RMB");
		System.out.println("6. 下单");
		/*
		用户输入6终止循环,如果不是6,继续循环
		*/
		while (choose != 6) {
			
			choose = sc.nextInt();
			
			switch (choose) {
				case 1:
					System.out.println("1. 啤酒 8RMB");
					total += 8;
					break;
				case 2:
					System.out.println("2. 方便面 88RMB");
					total += 88;
					break;
				case 3:
					System.out.println("3. 辣条 25RMB");
					total += 25;
					break;
				case 4:
					System.out.println("4. 烟 6RMB");
					total += 6;
					break;
				case 5:
					System.out.println("5. 可乐18RMB");
					total += 18;
					break;
				case 6:
					System.out.println("6. 下单");
					break;
				default:
					System.out.println("选择错误");
					break;
			}
		}	

		System.out.println("本场消费 : " + total);
	}
}

总结

  1. 循环过程中最核心的内容就是循环变量,需要对于循环变量的执行的过程中数值变量完全掌握!!!如果无法明确循环的次数,循环变量的值,循环的过程,可以将循环代码中变量的变更过程记录。
  1. 循环过程中需要注意无限循环问题,控制无限循环问题的出现。一定要保证循环条件有效性或者代码中存在一个跳出循环的机制。
  1. do while循环中,第一次循环体的执行是没有经过任何的条件判断的,需要注意!
    【已知,可控】
  1. while和do while循环结构是可以进行互换的。
  1. 然后在while和do while之间考虑使用的情况下,一般推荐使用while循环。但是不代表do while没有实际的使用效果。
发布了4 篇原创文章 · 获赞 6 · 访问量 197

猜你喜欢

转载自blog.csdn.net/qq_43571809/article/details/105586515