Java流程控制
用户交互Scanner
之前我们学的基本语法中我们并没禎实现程序和人的交互,但是 java 给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner
是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
基本语法:
1
| Scanner s = new Scanner(System.in);
|
通过 Scanner类的next()与 nextline()方法获取输入的字符串,在读取前我们一般需要使用 hasNext()与 hasNextLine()判断是否还有输入的数据。
练习
使用hasNext()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.everweekup.flowline;
import java.util.Scanner;
public class ScannerTest { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接受");
if(scanner.hasNext()) { String str = scanner.next(); System.out.println("输出的内容为:"+str); }
scanner.close(); }
}
|
1 2 3 4 5
| 使用next方式接受 hello world 输出的内容为:hello
Process finished with exit code 0
|
使用hasNextLine()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.everweekup.flowline;
import java.util.Scanner;
public class ScannerTest { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接受");
if(scanner.hasNextLine()) { String str = scanner.nextLine(); System.out.println("输出的内容为:"+str); }
scanner.close(); }
}
|
1 2 3 4 5 6
| 使用nextLine方式接受 hello world 输出的内容为:hello world
Process finished with exit code 0
|
next()
和 nextLine()
next
- 一定要读取到有效字符后オ可以结束输入。
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
- 只有输入有效字符后オ将其后面输入的空白作为分隔符或者结束符。
- next()不能得到带有空格的字符串。
nextLine
- 以 Enter为结束符,也就是说 next Line(方法返回的是输入回车之前的所有字符。
- 可以获得空白.
Scanner进阶使用
案例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
| package com.everweekup.flowline;
import java.util.Scanner;
public class ScannerType { public static void main(String[] args){ Scanner scanner = new Scanner(System.in);
int i = 0; float f = 0.0f;
System.out.println("请输入整数:");
if(scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println("整数数据:" + i); }else{ System.out.println("输入的不是整数数据"); }
System.out.println("请输入小数:"); if(scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("小数数据:" + f); }else{ System.out.println("输入的不是小数数据"); } } }
|
1 2 3 4 5 6
| 请输入整数: 1 整数数据:1 请输入小数: 1.2 小数数据:1.2
|
案例2
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
| package com.everweekup.flowline;
import java.util.Scanner;
public class ScannerSum { public static void main(String[] args){ Scanner scanner = new Scanner(System.in);
double sum = 0; int m = 0;
while(scanner.hasNextDouble()){ double x = scanner.nextDouble(); m += 1; sum += x; } System.out.println(m+"个数的和为: "+sum); System.out.println(m+"个数的平均为: "+sum/m); scanner.close(); } }
|
顺序结构
JAVA的基本结构就是顺序结构,除非特別指明,否则就按照顺序一句一句执行。顺序结构是最简单的算法结构。我们日常写的每行代码,都是按照从上往下的顺序,依次执行的。

语句与语句之间,框与框之间都是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
选择结构
if单选择结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class SingalIfStruct { public static void main(String[] args){ Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容: "); String s = scanner.nextLine();
if (s.equals("Hello")){ System.out.println(s); }
System.out.println("End"); scanner.close(); } }
|
if双选择结构
语法

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class DoubleIfStruct { public static void main(String[] args){ Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: "); double score = scanner.nextDouble();
if(score>60){ System.out.println("恭喜及格!"); }else{System.out.println("再接再厉");} } }
|
if多选择结构

语法
1 2 3 4 5 6 7 8 9
| if(布尔表达式 1){ }else if(布尔表达式 2){ }else if(布尔表达式 3){ }else{ }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class MultiIfStruct { public static void main(String[] args){ System.out.println("请输入成绩: ");
Scanner scanner = new Scanner(System.in); double score = scanner.nextDouble(); if(score>=90){ System.out.println("优秀!"); }else if(score>=80 && score < 90){ System.out.println("不错!"); }else if(score >=60 && score <80){ System.out.println("再接再厉"); }else if(score >=0 && score <60){ System.out.println("不及格了!"); }else{ System.out.println("成绩不明"); } } }
|
总结
if语句至多有1个eLse语句,else 语句在所有的 else if 語句之后。if 语句可以有若于个 eLse if 語句,它们必须在 else 语句之前。
一旦其中一个 else if 語句检測为 true,其他的 else if 以及 else 语句部将跳过执行。
嵌套的if结构
使用嵌套的 if else 语句是合法的。也就是说你可以在另ー个 if 或者 else if 语句中使用 if 或者 else if 语句。你可以像 if 语句一样嵌套 else if … else。
语法
1 2 3 4 5 6
| if(布尔表达式1){ if(布尔表达式2){ } }
|
案例
1.寻找一个数,在1~100之间
二分法查找
Switch多选择结构
多选择结构还有ー个实现方式就是 switch case
语句。switch case
语句判断一个变量与ー系列值中某个值是否相等,每个值称为ー个分支。
switch语句中的变量类型可以是:
- byte、 short、int或者char。
- 从 Java SE7开始,switch支持字符串 String类型了。
- 同时case标签必须为字符串常量或字面量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| switch(expression){ case value: break; case value: break; case value: break; default: }
|
Case穿透
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
| package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class SwitchStruct { public static void main(String[] args){ System.out.println("你考得怎么样?"); Scanner scanner = new Scanner(System.in); String status = scanner.nextLine();
switch (status){ case "及格": System.out.println("考的还行呢"); case "良好": System.out.println("emm,一般般吧..."); case "优秀": System.out.println("差点挂科了,欸"); case "满分": System.out.println("挂科了,太惨了..."); default: 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
| package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class SwitchStruct { public static void main(String[] args){ System.out.println("你考得怎么样?"); Scanner scanner = new Scanner(System.in); String status = scanner.nextLine();
switch (status){ case "及格": System.out.println("考的还行呢"); break; case "良好": System.out.println("emm,一般般吧..."); break; case "优秀": System.out.println("差点挂科了,欸"); break; case "满分": System.out.println("挂科了,太惨了..."); break; default: System.out.println("人与人之间的喜乐并不相同。"); } } }
|
平时想查看程序是怎么运行的,可以找到java程序编译生成的class字节码文件,通过将其拖到源文件程序所在的文件夹,利用IDEA打开即可查看反编译的文件内容。


可以得到反编译后的源码,可以发现,switch是通过hashcode()来匹配的。
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
package com.everweekup.SequentialStruct;
import java.util.Scanner;
public class SwitchStruct { public SwitchStruct() { }
public static void main(String[] args) { System.out.println("你考得怎么样?"); Scanner scanner = new Scanner(System.in); String status = scanner.nextLine(); byte var4 = -1; switch(status.hashCode()) { case 658856: if (status.equals("优秀")) { var4 = 2; } break; case 691634: if (status.equals("及格")) { var4 = 0; } break; case 900933: if (status.equals("满分")) { var4 = 3; } break; case 1058030: if (status.equals("良好")) { var4 = 1; } }
switch(var4) { case 0: System.out.println("考的还行呢"); break; case 1: System.out.println("emm,一般般吧..."); break; case 2: System.out.println("差点挂科了,欸"); break; case 3: System.out.println("挂科了,太惨了..."); break; default: System.out.println("人与人之间的喜乐并不相同。"); }
} }
|
循环结构
While循环
While是最基本的循环,它的语法结构为:
只要布尔表达式为true,循环就会一直执行下去。
我们大多数情况是会让循环停止下来,所以需要一个让表达式失效的方式来结束循环。
少部分情况需要循环一直执行,比如服务器的请求响应监听等。
循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能戓者造成程序卡死奔溃
案例
思考:计算1+2+3+.+-100=?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.everweekup.LoopStruct;
public class WhileStruct { public static void main(String[] args){
int loopc = 1; int sum = 0; while (loopc != 101){ sum += loopc; loopc ++; } System.out.println("1+2+...+100= "+ sum); } }
|
Do…While循环
对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件也至少执行依次。
do...while
循环和while循环相似,不同的是do...while
循环至少会执行一次。while是先判断后执行,do...while
是先执行后判断。
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.everweekup.LoopStruct;
import java.util.Scanner;
public class DoWhileStruct { public static void main(String[] args){ Scanner scanner = new Scanner(System.in);
System.out.println("选择你的身份!"); String life = scanner.nextLine();
do { System.out.println("出生"); System.out.println("死亡"); }while(life.equals("人"));
} }
|
注意,life变量的赋值不能放在do里面,while检测条件不会检测do代码框内。
For循环
虽然所有循环结构都可以用 while或者do. while表示,但Java提供了另ー种语句循环,使一些循环结构变得更加简单。
For循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
For循环执行的次数是在执行前就确定的。
语法格式如下:
1 2 3
| for(初始化; 布尔表达式; 更新){ }
|
练习
1.计算0到100之间的奇数和偶数的和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.everweekup.LoopStruct;
public class ForStruct { public static void main(String[] args){ int oddSum = 0; int evenSum = 0; for(int i=0; i<=100; i++){ if(i % 2 == 0){ evenSum += i; }else{oddSum += i;} } System.out.println("OddSum Is :" + oddSum); System.out.println("EvenSum Is :" + evenSum);
} }
|
2.用 whilei或就for循环输出1-1000之间能被5整除的数,并且每行输出3个
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void Loop1(){ int cnt = 0; for(int i=1; i<=1000; i++){
if(i % 5 == 0){ System.out.print(i+"\t"); cnt += 1; } if(cnt == 3){ System.out.println(); cnt = 0; } } }
|
3.打印九九乘法表
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void nnProduct(){ for(int i=1; i<=9; i++){ for(int j=1; j<=i; j++){ System.out.print(j+"*"+i+"="+(j*i)+"\t");
} System.out.println(); } }
|
4.打印三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.everweekup.LoopStruct;
public class TriangleDemo { public static void main(String[] args){ for (int i = 0; i <= 5; i++) { for (int j = 5; j>=i; j--){ System.out.print(" "); } for (int j =1; j<=i; j++){ System.out.print("*"); } for(int j=1; j<i; j++){ System.out.print("*"); } System.out.println(); } } }
|
idea里输入循环次数.for
再回车,即可快捷生成for循环语句。
增强型For循环
- 这里我们先只是见ー面,做个了解,之后数组我们重点使用
- Java5引入了ー种主要用于数组或集合的增强型for循环。
- Java增强for循环语法格式如下:
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.everweekup.LoopStruct;
public class EnhanceFor { public static void main(String[] args){ int [] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int i = 0; i < 10; i++) { System.out.print(numbers[i]+"\t"); } System.out.println(); System.out.println("========================"); System.out.println("增强型For循环"); System.out.println("========================");
for(int x : numbers){ System.out.print(x+"\t"); } } }
|
1 2 3 4 5 6
| 1 2 3 4 5 6 7 8 9 10 ======================== 增强型For循环 ======================== 1 2 3 4 5 6 7 8 9 10 Process finished with exit code 0
|
break&continue
break在任何循环语句的主体部分,均可用 break控制循环的流程。 break用于强行退出循环,不执行循环中剩余的语句。( break语句也在 switch语句中使用)
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
关于goto关键字
goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但井未在语言中得到正式使用;
Java没有goto。然而,在 break和 continue这两个关键字的身上,我们仍然能看出一些goto的影子-带标签的 break 和continue.
标签”是指后面跟一个冒号的标识符,例如:Iabel;
对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于 break和 continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
break练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.everweekup.LoopStruct;
public class BreakDemo { public static void main(String[] args){ int i = 0; while(i<10){ i++; System.out.print(i+"\t"); if(i==5){ break; }
} } }
|
break只是跳出循环,不会终止程序。
continue练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.everweekup.LoopStruct;
public class ContinueDemo { public static void main(String[] args){ int i = 0; while(i<10){ i++; if(i%2==0){ continue; } System.out.print(i+"\t");
} } }
|
1 2
| 1 3 5 7 9 Process finished with exit code 0
|
break在任何循环语句的主体部分,均可用break控制循环的流程。
break用于强行退出循环,不执行循环中剩余的語句。( break語句也在 Switch语句中使用)
continu语句用在循环语句体中,用于终止某次循环过程、即跳过循环体中尚未执行的語句,接着进行下一次是否执行循环的判定。