随笔day08
switch多选择结构
1.switch case 语句判断一个变量与一系列值中某一个值是否相等,每个值称为一个分支。
2.switch 语句中的变量类型可以是:
-
byte、short、int或者char
-
从Java SE 7开始
-
switch 支持字符串String类型
-
同时case标签必须为字符串常量或字面量
switch(expression){ case value : //语句 break;//可选 case value: //语句 break; //可选 //你可以有任意数量的case语句 default: //可选 //语句 }
例:
package com.hanzhubing.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//case 穿透 //switch 匹配一个具体的值
char grade='C';
switch (grade){
case 'A':
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;
default:
System.out.println("未知等级");
}
}
}
package com.hanzhubing.struct;
//字符的本质还是数字!!!
//反编译 java---class(字节码文件)---反编译(IDEA )
public class SwitchDemo02 {
public static void main(String[] args) {
String name="韩柱兵";
switch (name){
case "安琪拉":
System.out.println("安琪拉");
break;
case "TheShy":
System.out.println("TheShy");
break;
case "司空震":
System.out.println("司空震");
break;
case"韩柱兵":
System.out.println("韩柱兵");
break;
}
}
}
循环结构
- while循环
-
while(布尔表达式){
//循环内容
}
-
只要布尔表达式为true,循环就好一直执行下去
-
大多数情况是回趟循环停止下来,需要一个表达式失效的方式来结束循环
-
少部分情况需要循环一直执行,比如服务器的请求响应监听
-
循环条件一直为true就会造成无限循环[死循环],正常的业务编程中应该避免死循环。会影响程序性能或者造成程序卡死崩溃!
例:
package com.hanzhubing.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i=0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.hanzhubing.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+4....+100+=?
int i=0;
int sum=0;
while (i<=100){
sum=sum+i;
i=i+1;//i++
}
System.out.println(sum);
}
}
- do..while循环
-
不满足条件则不能进入循环,但有时需要不满足条件,也至少执行一次
-
do while 循环和while 循环相似,不同的是do while至少要执行一次
do{
//代码语句
}while(布尔表达式);
while和do while的区别
- while先判断后执行,do while是先执行后判断!
- do while总是保证循环体会被至少执行一次!这是它两的主要区别
-
for循环
- 虽然所有循环结构都是可以用while和do while表示,但java提供了另一种语句-----for循环,使循环结构变得更简单
- for循环语句支持迭代的一种通用结构,是最有效、最灵活的循环结构
- for循环执行的次数是在执行前就确定的。语法如下:
for(初始化;布尔表达式;更新){
//代码语句
}
例:
package com.hanzhubing.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a=1; //初始化条件
while (a<=100){
System.out.println(a);
a=a+2;//a+=2; 迭代
}
System.out.println("while循环结束");
//初始化值;条件判断;迭代
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
/*
for (int i = 0; i < 100; i++) { 快捷键100.for+enter必须在main方法中使用
*/
//死循环
for (; ; ) {
}
}
}
for循环注意点
最先执行初始化步骤,可以声明一种类型,但课初始化一个或多个循环控制变量,也可以是控语句。
然后,检测布尔表达式值。如果为true,循环体被执行。如果为false,循环中止,开始执行循环体后面的语句
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)
再次检测布尔表达式,循环执行上面过程。
例:
package com.hanzhubing.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习:计算0~100之间奇数和偶数的和
int oddsum=0;
int evensum=0;
for (int i = 0; i <= 100; i++) {
if (i%2==0){ //偶数
oddsum=oddsum+i;
}
else {
evensum=evensum+i; //奇数
}
}
System.out.println("偶数的和"+oddsum);
System.out.println("奇数的和"+evensum);
}
}
package com.hanzhubing.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习二:1~1000之间能被五整除,并且每行输入3个
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) { //能被五整除
System.out.print(i + "\t"); //输出结果不换行 \t类似tab效果,空了一格
}
if (i%(5*3)==0){
System.out.println();//输出结果后换行
// System.out.print(\n); 输出结果后不换行, \n换行符
}
}
}
}
package com.hanzhubing.struct;
/*
九九乘法表
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
*/
public class ForDemo04 {
public static void main(String[] args) {
//1.先打印第一列
//2.把固定的的1再用一个循环包起来
//3.去掉重复项,i<=j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <=j ; i++) {
System.out.print(i+"*"+j+"="+(j*i)+"\t");//输出结果后不换行空一格 \t tab
}
System.out.println();// 没输出玩一列后在进行换行
}
}
}
-
增强型for循环
-
java 5引入了一种主要用于数组集合的增强型for循环
-
java增强for循环语法如下:
for(声明语句:表达式)
{
//代码句子
}
- 声明语句:声明新的局部变量,该变量类型必须和数组元素的类型匹配。其作用域限定在循环的语句块,其值与此时数组元素的值相等
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法
例:
package com.hanzhubing.struct; public class ForDemo05 { public static void main(String[] args) { int[] numbers={10,20,30,40,50};//定义了一个数组 for (int i=0;i<5;i++){ System.out.println(numbers[i]); } System.out.println("=================================="); for (int x:numbers){ System.out.println(x); } } }
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!