Java 第四天

写在开头

 所有知识点都写下来实在是太多啦,而且有的知识点还是比较容易理解的,从今天开始我罗列出一天所学的知识点,比较容易理解的直接贴一个我觉得解释的比较全面的网址。
 嗯就这样!不是我懒!
 里面的知识点不可能都展现在标题,大家使用搜索功能(已经修复好了,还加了个萌萌的板娘)搜自己想要了解的知识点。

流程控制语句 switch case

switch…case语法结构

switch…case语法结构:

switch(表达式){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;

default:
default语句;
break;
}

说明:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

用代码说话:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//switch...case 语句
import java.util.Scanner;
//import java.util.Random;
public class SwitchCaseDemo
{
public static void main(String[] args)
{
//需求: 键盘输入一个整数(1-7),判断这是星期几
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个代表星期的整数:");
int xq = sc.nextInt();
/*
switch中的表达式xq与下面的case匹配,匹配成功,执行对应case的内容,
匹配不成功,执行default中的内容
*/
switch(xq){// xq的类型可以是: byte short char int String
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期七");
break;
default:
System.out.println("输入错误");
break;
}
}

ase语句后,break,不是必须的(那么switch就有穿透性),switch...case语句具有穿透性

举个栗子:键盘输入一个整数(1-7),匹配1-5,输出工作日,6-7输出休息日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// switch的穿透性
import java.util.Scanner;
public class SwitchChuanTou
{
public static void main(String[] args)
{
//要求: 键盘输入一个整数(1-7),匹配1-5,输出工作日,6-7输出休息日
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个代表星期的整数:");
int xq = sc.nextInt();
/*
switch...case的穿透性,
穿透性指: switch在进行case匹配时,匹配成功,但是没有遇到break,也没有遇到右大括号,就继续向下执行逻辑,直到遇到break或者右大括号,switch结束
*/
switch(xq){// byte short char int String
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");
break;
case 6:
case 7:
System.out.println("休息日");
break;
default:
System.out.println("输入错误");
break;
}
}
}

循环语句

for循环语句的语法格式

for关键字,循环
循环: 指同一个逻辑,需要多次执行,使用循环
举例: 登录界面,客户姓名,客户密码,姓名和密码都正确,登录成功
如果姓名或密码输入错误了,给你三次机会,这三次机会就可以使用循
环来完成这个功能

for的语法格式:
for(初始化值(定义变量); 判断语句; 变量值的变化(自增或自减))
多次执行的语句;

说明:

  1. 初始化值 int i = 0;
  2. 判断语句,必须是boolean类型的逻辑,i < 10
  3. 变量值的变化i++
    for(int i = 0; i < 10;i++){// 功能: 能将0-9的整数打印出来
    System.out.println(i);
    }
  4. 只要判断语句为true,那么for循环中的语句就会执行

for循环的执行过程

for循环的执行过程

for 循环的练习

1
2
3
4
5
6
7
8
9
10
11
12
13
// 打印数字5-1
public class ForPrintDemo
{
public static void main(String[] args)
{
// for 起始值 5 ,判断条件 >=1 , 变化 --
// for循环内部定义的变量 int i = 5; 只能在循环的大括号中使用
for(int i = 5 ; i >= 1 ; i --){
System.out.println(i);
}
// System.out.println("-----------"+i);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//计算1+2+3+...+100的和
// 1+2 = 3+3=6+4=10...+100 = 5050
public class Print1To100
{
public static void main(String[] args)
{
// sum值作为1-100的总和
int sum = 0;
//for 起始值1, <=100 , ++
for(int i = 1 ; i <= 100 ; i++){
/*
1 : 0+1 = 1 = sum i = 2
2: sum = 1 + 3 = 3; i = 3
3: sum = 3 + 3 = 6; i = 4
4: sum = 6 + 4 = 10
....

*/
sum = sum + i;
}
System.out.println(sum);

}
}

while 循环

while循环语法结构:
while(表达式){
while语句;
}

说明:

  1. 表达式必须是boolean类型
  2. 表达式为true,那么执行大括号中的while语句
  3. 表达式为false,那么while大括号中的语句不执行,while结束
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//打印数字5-1 (while)
// while循环语句
public class whileDemo
{
public static void main(String[] args)
{
// 打印数字5-1
/*for(int i = 5 ; i >=1 ; i --){
System.out.println(i);
}*/

int i = 5;
while(i >=1){// 判断条件 i只要大于等于1,那么while内的逻辑就执行
System.out.println(i);
i--;
}

}
}

for和while的使用分析:

  1. for一般使用于值的变化规律性强,可预计循环次数
  2. while使用,一般是不明确知道循环的次数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    /*
    要求: 珠穆朗玛峰高8848米,现在有一张纸0.01米,问,这张纸折多少次能达到珠穆朗玛峰的高度
    double paper = 0.01;
    int count = 0;
    while( paper <= 8848){
    paper = paper* 2;
    count++;
    }
    打印count*/
    //输入一个高度,求一张0.01的纸折多少次可能要这个高度。
    import java.util.Scanner;
    public class P1
    {
    public static void main (String[] args)
    {Scanner sc =new Scanner(System.in);
    //或者高度。
    System.out.println("请输入一个整数作为高度:");
    int m=sc.nextInt();
    //定义一个整数作为折纸的次数、
    int c=0;
    double z =0.01;
    while(z<=m)
    {
    z=z*2;
    c++;
    }
    System.out.println("折纸的次数是:"+c);

    }
    }

用代码说一下while循环的注意事项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// while循环的注意事项
public class WhileZhuYi
{
public static void main(String[] args)
{
boolean flag = false;
while(flag){
System.out.println("Hello World!");
}

/* 错误的语句
while(false){ // 无法访问的语句: while中的代码,永远不会被执行到(访问到),因此编译时报错
System.out.println("Hello World!");
}*/

/*while (true) //这是可以运行的,是死循环,就是循环永远没有结束的时候
{

}*/
}
}

while 循环

do : 关键字,做,操作,实行
while : 关键字,循环

do…while的语法结构:

do{
循环执行的语句;
}while(表达式);

说明:

  1. do语句,用来执行循环的,
    进入到do…while中,那么do里面的内容,先执行一次
    然后,判断,while中的表达式结果,如果为true,那么就继续执行do中的内容
    如果为false,那么dowhile就结束
  2. do..while 和while的区别
    do..while 首先必须先执行一次循环体
    while循环体的执行与否与表达式有关系,如果表达式直接为false,那么while一次都不执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// do while 循环
public class DoWhileDemo
{
public static void main(String[] args)
{
int i = 0;
// do...while语句,不管while中的表达式为true或者false,都先执行一次循环
do
{
System.out.println("+++++++++" + i);
}
while ( i > 6);

// while判断条件为false,while一次都不执行
while( i > 6){
System.out.println("----"+i);
}
}
}

晚安

 今天就到这里了,明天见,加油!

-------------本文结束感谢您的阅读-------------
0%