Java流程控制
1、用户交互Scanner
- next():
- 一定要读取到有效字符才可以结束输入
- 对输入有效字符之前遇到的空白,用next()方法会自动将其去掉
- 只有输入有效字符后才将其后面的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串 ```java package com.Frink.base.Scanner;
import java.util.Scanner;
public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(“使用next方式接收:”); //判断用户有没有输入字符串 if (scanner.hasNext()){ //使用next方式接收 String str = (String) scanner.next(); System.out.println(“输出的内容为:”+str); } //凡是属于IO流的类不关闭就会一直占用资源 scanner.close(); } } 使用next方式接收: java javac 输出的内容为:java
Process finished with exit code 0
- nextLine()1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符1. 可以获得空白```javapackage com.Frink.base.Scanner;import java.util.Scanner;public class Demo01 {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() ;}}使用nextLine方式接收:java javac输出的内容为:java javacProcess finished with exit code 0
Scnner进阶用法
用于求和以及计算平均值的案例
package com.Frink.base.Scanner;import java.util.Scanner;public class Demo05 {public static void main(String[] args) {//可以输入多个数字,求个并计算平均值,输入一个数字用回车确认,并通过非数字来结束运算Scanner scanner = new Scanner(System.in);double sum = 0;//计算输入了多少个数字int m = 0;//通过循环来判断是否还还有输入。并在里面进行一次求和和统计while (scanner.hasNextDouble()){double v = scanner.nextDouble();m = m + 1;//m = m +1sum = sum + v;System.out.println("你输入了第"+m+"个数据,当前结果sum="+sum);}System.out.println(m + "个数的和为" + sum);System.out.println(m + "个数的平均值是" + (sum / m));scanner.close();}}10你输入了第1个数据,当前结果sum=10.030你输入了第2个数据,当前结果sum=40.050你输入了第3个数据,当前结果sum=90.0x3个数的和为90.03个数的平均值是30.0Process finished with exit code 0
2、顺序结构
- Java的基本结构就是顺序结构,除非热别指明,否则就按照顺序一句一句执行。
- 顺序结构是Java最简单的算法结构
- 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,他是任何一个苏三发都离不开的一种基本算法结构
3、选择结构
if单选择结构
在语句中用if语句去判断这一个语句是否可行
语法:
```java
public class IfDemo01 {
public static void main(String[] args) {
} }Scanner scanner = new Scanner(System.in);System.out.println("请输入内容:");String s = scanner.nextLine();//equals:用于判断字符串是否相等if (s.equals("Hello")){System.out.println(s);}System.out.println("End");scanner.close();
<a name="e7PKE"></a>### if双选择结构语法<br /><br />```javapublic class IfDemo02 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入成绩:");int score = scanner.nextInt();if (score>60){System.out.println("及格");}else {System.out.println("不及格");}scanner.close();}}
公司要收购一个软件,成功了:给人支付100万元,失败了:自己找人开发
public class Excise03 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("是否成功:");int succse = scanner.nextInt();if (succse == 1 ){System.out.println("支付100万元");}else {System.out.println("自己开发");}scanner.close();}}
if多选择结构
语法
public class IfDemo03 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入成绩");int score = scanner.nextInt();if (score==100){System.out.println("S");}else if (score<100 && score>=90){System.out.println("A");}else if (score<90 && score>=80){System.out.println("B");}else if (score<80 && score>=70){System.out.println("C");}else if (score<70 && score>=60){System.out.println("D");}else if (score<60 && score>=0){System.out.println("E");}else{System.out.println("成绩不合法");}scanner.close();}}
嵌套循环
switch多选择结构
- 多选择结构还有一个实现方式是swit case语句
- swit语句判断一个变与一些列值中的某个值是否相等,每个值成为一个分支

switch语句中的变量可以是:
byte、short、int、char ```java public class SwitchDemo01 { public static void main(String[] args) { //case穿透 switch匹配一个值 char grade = ‘E’; 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("未知等级");
}
} }
- **switch同时支持String**```javapublic class SwitchDemo02 {public static void main(String[] args) {String name = "YANG";//表达式的结果可以是字符串//字符的本质还是数字//反编译Java---class(字节码文件)----反编译(IDEA)switch (name){case "JIDA":System.out.println("JIDA");break;case "FrinkYang":System.out.println("FrinkYang");break;default:System.out.println("HUI");}}}
编译结果
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//package com.Frink.Struct;public class SwitchDemo02 {public SwitchDemo02() {}public static void main(String[] args) {String name = "YANG";byte var3 = -1;switch(name.hashCode()) {case 2276860:if (name.equals("JIDA")) {var3 = 0;}break;case 1093499227:if (name.equals("FrinkYang")) {var3 = 1;}}switch(var3) {case 0:System.out.println("JIDA");break;case 1:System.out.println("FrinkYang");break;default:System.out.println("HUI");}}}
-
4、循环结构
while循环
while是最基本的循环,结构为:

只要布尔表达式为true。循环就会一直执行下去
- 我们大多是情况下是会让循环停止下来的,只需要一个让表达式失效的方式来结束
- 少部分情况需要循环一直运行,不如服务器的请求响应监听等
循环条件一直为true就会造成无限循环,我们正常编程中不应该出现无限循环。会影响程序性能或者造成程序卡死崩溃
public class Excise04 {public static void main(String[] args) {int i = 0;int sum = 0;while (i<=100){sum = sum + i;i++;System.out.println(sum);}}}
do…whlie循环
对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件也要至少执行一次.
- do…while循环和while循环类似,不同的是,do…while循环至少会执行一次

- while和do..while的区别:- while先判断后执行。do...while是限制性后判断- do...while总是保证循环会至少被执行一次!
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循环
- 虽然所有循环都可以用d..whlie和while来表示,但Java提供了一种语句--For循环,是一些循环结构变得更加简单- for循环执行的次数是在执行前就确定的:

- for循环语句是支持迭代的一种通用接哦古,是最有效、最灵活的循环结构
public class ForDemo01 {public static void main(String[] args) {int a = 1;//初始化条件while (a<100){//判断条件System.out.println(a);//循环体a+=2;//迭代 a = a + 2}//初始化 //条件判断 //迭代for (int i=1;i<=100;i++){System.out.println(i);}System.out.println("=========================");for (int q=3;q<=10;q++){System.out.println(q);}}}
不确定次数循环
for(;;) // while(ture)
for语句练习
输出一个99乘法表
public class ForDemo04 {public static void main(String[] args) {for (int j = 0; j <= 9; j++) {for (int i = 1; i <= j; i++) {System.out.print(j+"*"+i+"="+(j*i) + "\t");}System.out.println();}}}
列出0-1500中可以被5整除的数,并且要求三个一行
public class Excise06 {public static void main(String[] args) {for (int i = 0; i <=1000; i++) {if (i%5==0){System.out.print(i+"\t");}if (i%15==0){System.out.println();}}}}
求0-100中奇数和偶数的和 ```java public class ExciseDemo05 { public static void main(String[] args) {
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);
} }
<a name="o0CGa"></a>## 5、break&continue<a name="V1dE3"></a>### breakbreak在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。```javapublic class BreakDemo01 {public static void main(String[] args) {int i = 1;while (i<100){i++;System.out.println(i);if (i==10){break;}}}}
continue
continue语句再循环语句体中,用于终止某次循环,即跳过循环体中尚未执行的语句,接着进行下一次是执行的判定
public class ContinueDemo01 {public static void main(String[] args) {int i = 0;while (i<100){i++;if (i%10==0){System.out.println();continue;}System.out.print(i);}}}
6、练习
输出一个三角形,行数为5
public class Excise08 {public static void main(String[] args) {for (int i = 1; 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();}}}
