Java流程控制(第三期)
switch多选择结构
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
语法
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break;//可选
//你可以有任意数量的case语句
default;//可选
//语句
}
switch语句中的变量类型可以是:
-
byte,short,int,或者char。
-
从Java SE 7 开始
-
switch 支持字符串 String 类型
-
同时case 标签必须为字符串常量和字面量。
一个好的程序员要有自己的思想,比如多去看源码。
要点1:
讲一个case穿透现象:
在一个switch case语句中,不写break就全输出了, 所以这里要根据需要选择在每个case里面加不加break switch 这个循环的意思就是去匹配一个值, 一个具体的值, 在众多case找到一个符合要求的
package com.gan.structure;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'F';
switch (grade) {
case 'A':
System.out.println("优秀");
break;//可选
//这题就很有必要在每个case语句串后面都要加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("未知(输入错误)");
}
}
}
要点2:
JDK7新特性,表达式结果可以是字符串!!!
package com.gan.structure;
public class SwitchDemo02 {
public static void main(String[] args) {
String name="西柚";
switch (name){
case "西柚":
System.out.println("西柚");
break;
case "暖小白":
System.out.println("暖小白");
break;
default:
System.out.println("爱叫啥叫啥");
}
}
}
要点3: 字符的本质还是数字 反编译 java---class(字节码文件)----反编译(IDEA) 每一个对象都要hashcode 哈希是通过特点算法,给每一个"东西"一个唯一的值 每一个对象都对应了一个hash值。
用IDEA打开class文件方法:
把一个class文件从文件夹层面 复制到idea存放java文件的文件夹下就能打开,idea会自动实现反编译
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.gan.structure;
public class SwitchDemo02 {
public SwitchDemo02() {
}
public static void main(String[] args) {
String name = "西柚";
byte var3 = -1;
switch(name.hashCode()) {
case 1117755:
if (name.equals("西柚")) {
var3 = 0;
}
break;
case 25998692:
if (name.equals("暖小白")) {
var3 = 1;
}
}
switch(var3) {
case 0:
System.out.println("西柚");
break;
case 1:
System.out.println("暖小白");
break;
default:
System.out.println("爱叫啥叫啥");
}
}
}
循环结构
while循环详解
需要一个程序运行多少次运行多久,不能停的
你访问淘宝,阿里啥的都是24小时全年无休的运行的
-
while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}
-
只要布尔表达式为true,循环就会一直执行下去。
-
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
package com.gan.structure;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while (i < 100) {//用i<100来结束
i++;
System.out.println(i);
}
}
} -
少部分情况需要代码一直执行,比如服务器的请求响应监听等。
-
循环一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。
会影响程序性能和造成程序卡死崩溃。
package com.gan.structure;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true) {
/*
等待客户端接入,比如微信等人消息
定时检查,比如闹钟
*/
}
}
} -
思考:计算1+2+3+4......+100=?
package com.gan.structure;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+...100
int i = 0;
int sum = 0;
while (i <= 100) {
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
do...while循环
对于while循环而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件, 也至少执行一次
-
do.....while循环和while循环相似,不同的是,do....while循环至少会执行一次
do{
//代码语言
}while(布尔表达式);
package com.gan.structure;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i=0;
int sum=0;
do {
sum=sum+i;
i++;
}while(i<=100);
System.out.println(sum);
}
} -
while和do...while的区别
while先判断后执行,dowhile是先执行后判断!
Do....while总是保证循环体至少执行一次。这是他们的主要区别。
package com.gan.structure;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a < 0) {
System.out.println(a);
a++;
}
System.out.println("=====================");
do {
System.out.println(a);
a++;
} while (a < 0);
}
}
For循环详解(重要)
虽然所有循环结构都可以用while和do....while表示,但Java提供了另一种语言——for循环,使一些循环变得更加简单。
-
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的结构
-
for循环执行的次数是在执行前就确定的。语法格式如下:
for(初始化;布尔表达式;更新){
//代码语句
}
关于for循环有以下几点说明: 1.最先执行初始化步骤。可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句
-
然后检测布尔表达式的值。如果为true,循环体就执行。如果为false,循环终止,开始执行循环体后面的语句
-
.再次检测布尔表达式,循环执行上面的过程。
-
.循环体执行后才迭代
-
总结:*按照初始化,布尔表达式检测,循环体,迭代的顺序执行
-
package com.gan.structure;
public class ForDemo01<or> {
public static void main(String[] args) {
int a = 1;//条件初始化
while (a <= 100) {//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("while循环结束!");
//初始化//条件判断//迭代
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
System.out.println("for循环结束!");
//死循环
for (; ; ) {
}
}
}
练习1:计算0到100之间的奇数和偶数的和
package com.gan.structure;
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i % 2 != 0) {
oddSum += i;
} else {
evenSum += i;
}
}
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
package com.gan.structure;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
}
if (i % (5 * 3) == 0) {
System.out.println();
//或System.out.print("\n");
}
}
//println 输出完换行
//print 输出完不换行
System.out.println("for循环完成!");
int a = 0;
while (a <= 1000) {
a++;
if (a % 5 == 0) {
System.out.print(a + "\t");
}
if (a % 15 == 0) {
System.out.println();
}
}
}
}
练习3 : 九九乘法表
1.我们先打印第一列,这个都会 2.我们把固定的1再用一个循环包起来 3.去掉重复项。i<=j 4. 整理样式
package com.gan.structure;
public class ForDemo04 {
public static void main(String[] args) {
//
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}