我是靠谱客的博主 舒心冰淇淋,这篇文章主要介绍JavaSE学习总结(二)键盘录入数据/流程控制语句——顺序结构/选择结构(if/switch)/循环结构(for/while/循环嵌套/控制跳转语句)一、键盘录入数据二、流程控制语句,现在分享给大家,希望可以做个参考。

一、键盘录入数据

  • 为什么要使用键盘录入数据
    (1)为了让程序的数据更符合开发的数据
    (2)让程序更灵活一点

  • 如何实现键盘录入呢?
    格式:
    (1)导包
    import java.util.Scanner;
    位置:
    在class上面。
    (2)创建键盘录入对象
    格式:
    Scanner sc = new Scanner(System.in);
    (3)通过对象获取数据
    格式:
    int x = sc.nextInt();

  • 案例演示:

  1. 键盘录入3个整数,求最大值。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a,b,c,max,max2; Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数:"); a=sc.nextInt(); System.out.println("请输入第二个数:"); b=sc.nextInt(); System.out.println("请输入第三个数:"); c=sc.nextInt(); max=a>b?a:b; max2=max>c?max:c; System.out.println("最大的数是:"+max2); } }

结果:
在这里插入图片描述

  1. 键盘录入两个整数,判断是否相等。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a,b; Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数:"); a=sc.nextInt(); System.out.println("请输入第二个数:"); b=sc.nextInt(); String c=a==b?"相等":"不相等"; System.out.println(c); } }

结果:
在这里插入图片描述

二、流程控制语句

  1. 什么是流程控制语句
    在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的。
    也就是说程序的流程对运行结果有直接的影响。所以,我们必须清楚每条语句的执行流程。
    而且,很多时候我们要通过控制语句的执行顺序来实现我们要完成的功能
  2. 流程控制语句的分类
    (1)顺序结构
    (2)选择结构
    (3)循环结构

(一)顺序结构

是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行,程序中大多数的代码都是这样执行的。总的来说:写在前面的先执行,写在后面的后执行

(二)选择结构

1.概述

也被称为分支结构。选择结构有特定的语法规则,代码要执行具体的逻辑运算进行判断,逻辑运算的结果有两个,所以产生选择,按照不同的选择执行不同的代码。

2.选择结构的分类

  • if语句
  • switch语句

2.1 if语句格式一

复制代码
1
2
3
4
if(比较表达式或者是boolean类型的值) { 语句体; }
  • 执行流程:
    先计算比较表达式的值,看其返回值是true还是false。
    如果是true,就执行语句体;
    如果是false,就不执行语句体;

  • 注意事项:

  1. 比较表达式无论简单还是复杂,结果必须是boolean类型
  2. if语句控制的语句体如果是一条语句,大括号可以省略;如果是多条语句,就不能省略。建议永远不要省略。

2.2 if语句格式二

复制代码
1
2
3
4
5
6
if(比较表达式) { 语句体1; }else { 语句体2; }
  • 注意事项:else后面是没有比较表达式的,只有if后面有。

  • 执行流程:
    首先计算比较表达式的值,看其返回值是true还是false。
    如果是true,就执行语句体1;
    如果是false,就执行语句体2;

  • 案例演示:

  1. 获取两个数据中较大的值
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入第一个整数"); int a=sc.nextInt(); System.out.println("请输入第二个整数"); int b=sc.nextInt(); int max; if(a>b){ max=a; }else{ max=b; } System.out.println("最大值是"+max); } }

结果:
在这里插入图片描述

  1. 获取三个数据中最大的值
复制代码
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
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a,b,c; Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数"); a=sc.nextInt(); System.out.println("请输入第二个数"); b=sc.nextInt(); System.out.println("请输入第三个数"); c=sc.nextInt(); int max; if(a>b){ if(a>c){ //if语句的嵌套使用 max=a; }else { max=c; } } else{ if(b>c){ max=b; }else{ max=c; } } System.out.println("最大值是:"+max); } }

结果:
在这里插入图片描述

  1. 判断一个数据是奇数还是偶数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a; Scanner sc=new Scanner(System.in); System.out.println("请输入一个整数:"); a=sc.nextInt(); if(a%2==0){ System.out.println("这个数是偶数"); }else{ System.out.println("这个数是奇数"); } } }

在这里插入图片描述

2.3 if语句格式三

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; }
  • 执行流程:
    首先计算比较表达式1看其返回值是true还是false,
    如果是true,就执行语句体1,if语句结束。
    如果是false,接着计算比较表达式2看其返回值是true还是false,
    如果是true,就执行语句体2,if语句结束。
    如果是false,接着计算比较表达式3看其返回值是true还是false,

    如果都是false,就执行语句体n+1。

  • 注意事项: 当只要有一个条件满足时,if语句结束。else是可以省略,但是不建议省略。

  • 案例演示

  1. 键盘录入一个成绩,判断并输出成绩的等级。
    90-100 优秀
    80-89 良
    70-79 中
    60-69 及格
    0-59 差
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a; Scanner sc=new Scanner(System.in); System.out.println("请输入成绩:"); a=sc.nextInt(); if(a>=90&&a<=100){ System.out.println("成绩等级为:优秀!"); }else if(a>=80&&a<=89){ System.out.println("成绩等级为:良好!"); }else if(a>=70&&a<=79){ System.out.println("成绩等级为:中等!"); }else if(a>=60&&a<=69){ System.out.println("成绩等级为:及格!"); }else if(a>=50&&a<=59){ System.out.println("成绩等级为:不及格!"); }else{ System.out.println("成绩不合法!"); } } }

结果:
在这里插入图片描述
在这里插入图片描述

  1. 键盘录入x的值,计算出y的并输出。
    x>=3 y = 2x + 1;
    -1<=x<3 y = 2x;
    x<-1 y = 2x – 1。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int x,y; Scanner sc=new Scanner(System.in); System.out.println("请输入x的值:"); x=sc.nextInt(); if(x>=3){ y=2*x+1; }else if(x>=-1&&x<3){ y=2*x; }else{ y=2*x-1; } System.out.println("y的值为:"+y); } }

结果:
在这里插入图片描述

2.4 switch语句

  • 格式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(表达式){ case1: 语句体1; break; case2: 语句体2; break; case3: 语句体3; break; .... default: 语句体n+1; break; }
  • 执行流程:
    先计算表达式的值
    然后和case后面的匹配,如果匹配成功就执行对应的语句,遇到break就会结束switch语句,匹配不成功执行default控制的语句。
  • 案例演示:
复制代码
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
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args){ int a; Scanner sc=new Scanner(System.in); System.out.println("请输入一个0-4的整数获奖码以查看奖励"); a=sc.nextInt(); switch(a){ case 1: System.out.println("恭喜获得一台笔记本电脑!"); break; case 2: System.out.println("恭喜获得一部手机!"); break; case 3: System.out.println("恭喜获得100元红包!"); break; case 4: System.out.println("恭喜获得5元优惠券!"); break; default: System.out.println("输入不合法!"); break; } } }

结果:
在这里插入图片描述

  • 注意事项:
    (1)switch所支持的数据类型:byte,short,int,char
    JDK5以后可以是枚举类型
    JDK7以后可以是String
    (2)case后面跟的是要和表达式进行比较的值,必须是常量,而且多个case后面的值不能相同。
    (3)语句体部分可以是一条或多条语句
    (4)break表示中断,结束的意思,可以结束整个switch语句,如果忘了写一个break,则会出现"case穿透"现象:会多执行下一个case的语句体。
    (5)default语句表示所有情况都不匹配的时候,就执行该处的内容,和if语句的else相似,default语句位置不一定放在最后,放在哪都可以default可以省略,但是不建议,因为它的作用是对不正确的情况给出提示。
    (6)switch语句的结束条件
    1.遇到break就结束了
    2.执行到末尾就结束了

  • 面试题:
    byte类型数据可以作为switch的表达式吗? 可以
    long类型数据可以作为switch的表达式吗? 不可以
    String类型数据可以作为switch的表达式吗? JDK7以后可以

看程序写结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x = 2; int y = 3; switch(x){ case 2: y++; case 3: y++; case 4: y++; break; default: y++; break; } System.out.println("y="+y);

因为case 2和case 3后面都没写break;因此出现了"case穿透"现象,所以y自增了三次。
在这里插入图片描述

2.5 if语句和switch语句的区别

  • 案例演示
    分别用switch语句和if语句实现下列需求:
    键盘录入月份,输出对应的季节
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner; class ScannerDemo{ public static void main(String[] args) { int x; Scanner sc=new Scanner(System.in); System.out.println("请输入月份(1-12的整数):"); x=sc.nextInt(); if(x==3||x==4||x==5){ System.out.println("春季"); }else if(x==6||x==7||x==8){ System.out.println("夏季"); }else if(x==9||x==10||x==11){ System.out.println("秋季"); }else if(x==12||x==1||x==2){ System.out.println("冬季"); }else{ System.out.println("输入不合法!"); } } }
复制代码
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
import java.util.Scanner; class ScannerDemo1{ public static void main(String[] args){ int x; Scanner sc=new Scanner(System.in); System.out.println("请输入月份(1-12的整数):"); x=sc.nextInt(); switch(x){ case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; case 12: case 1: case 2: System.out.println("冬季"); break; default: System.out.println("输入不合法!"); break; } } }
  • 在做判断的时候,我们有两种选择,if语句和switch语句,那么,我们到底该如何选择使用那种语句呢?

    if语句使用场景:
    (1)针对结果是boolean类型的判断
    (2)针对一个范围的判断
    (3)针对几个常量值的判断
    switch语句使用场景:
    (1)针对几个常量值的判断

(三)循环结构

1.概述

循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环。

2.分类

  • for循环
  • while循环
    • while语句
    • do…while语句

2.1 for循环

  • 格式:
复制代码
1
2
3
4
for(初始化表达式语句;判断条件语句;控制条件语句) { 循环体语句; }
  • 执行流程:
    (1)执行初始化表达式语句
    (2)执行判断条件语句,看其返回值是true还是false
    如果是true,就继续执行
    如果是false,就结束循环
    (3)执行循环体语句;
    (4)执行控制条件语句
    (5)回到(2)继续。
  • 注意事项:
    初始化表达式语句只执行一次
  • 案例演示:
  1. 求出1到10的整数累加和
复制代码
1
2
3
4
5
6
7
8
9
10
public class ForDemo1 { public static void main(String[] args) { int sum=0; for(int i=1;i<=10;i++){ sum+=i; } System.out.println(sum); } }

结果:
在这里插入图片描述

  1. 分别求出1-100的偶数之和和奇数之和
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class ForDemo2 { public static void main(String[] args) { int sum1=0; int sum2=0; for(int i=1;i<=100;i++){ if(i%2==0)sum1+=i; else sum2+=i; } System.out.println("偶数和是"+sum1+",奇数和是"+sum2); } }

结果:
在这里插入图片描述

3.在控制台输出所有的”水仙花数”,并统计其个数
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
举例:153就是一个水仙花数。
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ForDemo3 { public static void main(String[] args) { int a,b,c,count=0; System.out.println("水仙花数:"); for(int i=100;i<=999;i++){ a=i%10; b=i/10%10; c=i/100%10; if(a*a*a+b*b*b+c*c*c==i) { System.out.println(i); count++; } } System.out.println("水仙花数一共有:"+count+"个"); } }

结果:
在这里插入图片描述

2.2 while循环

2.2.1 while语句
  • 格式:
复制代码
1
2
3
4
5
6
初始化条件语句; while(判断条件语句) { 循环体语句; 控制条件语句; }
  • 执行流程:
    (1)执行初始化条件语句;
    (2)执行判断条件语句,看其返回值是true还是false
    如果是true,就继续执行
    如果是false,就结束循环
    (3)执行循环体语句;
    (4)执行控制条件语句
    (5)回到(2)继续。
  • 注意事项:
    省略控制条件语句会出现“死循环”。
  • 案例演示
    求出1到10的整数累加和
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class WhileDemo { public static void main(String[] args) { int i=1; int sum=0; while(i<=10){ sum+=i; i++; } System.out.println(sum); } }
2.2.2 do…while语句
  • 格式:
复制代码
1
2
3
4
5
6
初始化条件语句; do { 循环体语句; 控制条件语句; }while(判断条件语句);//注意:有分号
  • 执行流程:
    (1)执行初始化条件语句;
    (2)执行循环体语句;
    (3)执行控制条件语句;
    (4)执行判断条件语句,看其返回值是true还是false
    如果是true,就继续执行
    如果是false,就结束循环
    (5)回到(2)继续。

  • 案例演示
    请在控制台输出数据1-10

复制代码
1
2
3
4
5
6
7
8
9
10
public class DoWhileDemo { public static void main(String[] args) { int i=1; do{ System.out.println(i); i++; }while(i<=10); } }

结果:
在这里插入图片描述

  • 三种循环语句的区别:
    do…while循环至少执行一次循环体。
    而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
  • 注意事项:
    一定要注意写控制条件语句,否则就容易死循环。
    两种最简单的死循环格式:
复制代码
1
2
3
while(true){...} for(;;){...}

2.3 循环嵌套

案例演示

  1. 需求:请输出一个4行5列的星星(*)图案。一次只能打印一个*
    如图:
    *****
    *****
    *****
    *****
    思路:
    外循环控制行数,内循环控制列数
复制代码
1
2
3
4
5
6
7
8
9
10
11
public class Demo{ public static void main(String[] args) { for(int i=0;i<4;i++){ for(int j=0;j<5;j++){ System.out.print("*");//注意println和print的区别 } System.out.println();//仅仅输出一个换行 } } }

结果:
在这里插入图片描述

  1. 在控制台输出九九乘法表。
复制代码
1
2
3
4
5
6
7
8
9
10
11
public class Demo{ public static void main(String[] args) { for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ System.out.print(i+"x"+j+"="+i*j+" "); } System.out.println(); } } }

结果:
在这里插入图片描述

2.4 控制跳转语句

  • 什么是控制跳转语句
    前面我们已经说过了,Java中的goto是保留字,目前不能使用。虽然没有了goto语句可以增强程序的安全性,但是也带来很多不便,比如说,我想在某个循环执行到某一步的时候就结束,现在就做不了这件事情。为了弥补这个缺陷,Java就提供了break,continue和return来实现控制语句的跳转和中断。
  • 控制跳转语句的分类
    break 中断
    continue 继续
    return 返回
2.4.1 break语句
  • break的使用场景
    (1):在选择结构switch语句中
    (2):在循环语句中
    注意:离开使用场景的存在是没有意义的
  • 作用
    (1)退出单层循环
    (2)退出多层循环(几乎不使用)
    要想实现这个效果,就可以用标签给语句起别名;
    格式:
    标签名:语句
    break 标签名
  • 案例演示
  1. 退出单层循环
复制代码
1
2
3
4
5
6
7
8
9
10
11
public class Demo { public static void main(String[] args) { for (int i = 1; i < 100; i++) { System.out.println(i); if(i==10){ break; } } } }

结果
在这里插入图片描述

  1. 退出多层循环

方法一:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Demo { public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.println("内层循环"+j); if(j==2){ break;//(1) } } System.out.println("跳出一次内层循环"); if(i==1){ break;//(2) } } System.out.println("结束外层循环"); } }

结果
在这里插入图片描述
(1)处的break跳出的是内循环,(2)处的break跳出的是外循环。也就是说,一个break只能跳出一层循环。该方法用了两个break才跳出双层循环,方法二则用一个break达到退出双层循环的效果:

方法二:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Demo { public static void main(String[] args) { one:for (int i = 0; i < 10; i++) { //利用前面提到的用标签起别名的方法 two:for (int j = 0; j < 10; j++) { System.out.println("内层循环"+j); if(j==2){ break one;//break + 别名 直接退出了外层循环 } } System.out.println("跳出一次内层循环"); if(i==1){ break; } } System.out.println("结束外层循环"); } }

结果
在这里插入图片描述

2.4.2 continue语句
  • continue的使用场景
    (1):在循环语句中
    注意:离开使用场景的存在是没有意义的
  • 作用
    跳出一次循环,执行下一步操作
  • 案例演示
    输出1-10中的奇数
复制代码
1
2
3
4
5
6
7
8
9
10
11
public class Demo { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if(i%2==0){ continue; } System.out.println(i); } } }

结果
在这里插入图片描述
解释:当 i 对2取余数为0时,即为偶数时,跳过这一次循环,进入下一次循环,从而跳过偶数的输出,只输出了奇数。

  • 练习:
    依次在注释处填写一句话的代码,达到以下需求:
    (1)在控制台输出2次:“Hello World“
    (2)在控制台输出7次:“Hello World“
    (3)在控制台输出13次:“Hello World“
复制代码
1
2
3
4
5
6
7
for(int x=1; x<=10; x++) { if(x%3==0) { //在此处填写代码 } System.out.println("Hello World"); }

答:
(1)break;
(2)continue;
(3)System.out.println(“Hello World”);

2.4.3 return语句
  • return的作用
    return 就是退出一个方法。
    跳转到上层调用的方法。这个在后篇方法的使用会再详细的讲解。
  • 三种控制跳转语句区别
    return: 结束当前方法
    break: 结束单层循环
    continue: 结束当前循环,执行下一次循环

最后

以上就是舒心冰淇淋最近收集整理的关于JavaSE学习总结(二)键盘录入数据/流程控制语句——顺序结构/选择结构(if/switch)/循环结构(for/while/循环嵌套/控制跳转语句)一、键盘录入数据二、流程控制语句的全部内容,更多相关JavaSE学习总结(二)键盘录入数据/流程控制语句——顺序结构/选择结构(if/switch)/循环结构(for/while/循环嵌套/控制跳转语句)一、键盘录入数据二、流程控制语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部