2.Java流程控制
1.用户交互Scanner
Java提供了一个工具类,可以获取用户的输入。java.util.Scanner,可以通过Scanner类来获取用户的输入。
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
- next()
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将其后面的空白作为分隔符或者结束符
- next() 不能得到带有空格的字符串
// 创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
// 判断用户有没有输入字符串
if (scanner,hasNext()){
// 使用next方式接收
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
// 凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉。
scanner.close();
- nextLine()
- 以Enter为结束符,也就是说nextLine() 方法返回的是输入回车之前的所有字符
- 可以获得空白
// 用户空格之后的依旧输出,使用hasNextLine()
if (scanner,hasNextLine()){
// 判断是否还有输入
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
}
- 不用判断也可以进行输入输出 程序直接顺序执行
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
scanner.close();
- 练习1 改程序本地测试有些问题
public class Demo01 {
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.hasNextInt();
System.out.println("整数数据:" + i);
}else {
System.out.println("输入的不是整数数据!");
}
System.out.println("请输入小数:");
// 如果 那么
if (scanner.hasNextFloat()){// 判断你输入的是否还有整数
f = scanner.hasNextInt();
System.out.println("小数数据:" + f);
}else {
System.out.println("输入的不是小数数据!");
}
scanner.close();
}
}
-
练习2
我们可以输入多个数字,并求其总和与平均值,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
public class Demo02 {
public static void main(String[] args) {
// 我们可以输入多个数字,并求其总和与平均值,每输入一个数字用回车确认,
// 通过输入**非数字**来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
// 和
double sum = 0;
// 计算输入了多少个数字
int m = 0;
System.out.println("请输入数据:");
// 通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
m = m + 1;
sum = sum + x;
// 每输入一个数据,计算当前为第几个输入的数据,然后计算实时的总和
System.out.println("你输入了第" + "个数据,当前的结果sum=" + sum);
}
System.out.println(m + "个数的和为:" + sum);
System.out.println(m + "个数的平均值为:" + (sum / m));
scanner.close();
}
}
2.顺序结构
- Java的最基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
- 顺序结构是最简单的算法结构。
- 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由软干戈异常执行的处理步骤组成的。他是任何一个算法都离不开的一种基本算法结构。
3.选择结构
-
if单选择结构
- 我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示。
if(布尔表达式){ // 如果布尔表达式为true将执行的语句 }- 案例
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(); } } -
if双选择结构
- 现在有个需求,公司要收购个软件,成功了给人支付100w,失败了,自己找人开发。这样的需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构,所以就有个if-else结构
if(布尔表达式){ // 如果布尔表达式的值为true }else { // 如果布尔表达式的值为false }- 案例
public class IfDemo02 { public static void main(String[] args) { // 考试分数大于60分就是及格,小于60分就是不及格 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(); } } -
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("你的成绩为满分!"); }else if (score<100 && score>=90){ System.out.println("你的成绩为优秀!"); }else if (score<90 && score>80){ System.out.println("你的成绩为良好!"); }else if (score<80 && score>=60){ System.out.println("你的成绩为合格!"); }else if (score<60 && score>=0){ System.out.println("你的成绩为不合格!"); }else { System.out.println("你输入的分数不符合规则!"); } scanner.close(); } }- 注意
- 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.
-
switch多选择结构
-
多选择结构还有一个实现方法就是switch case语句。
-
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
-
switch语句中的变量类型可以是:
- byte、short、int或者char
- 从Java SE 7开始支持字符串String类型
public class SwitchDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("您在项目中担任的合同方为:"); String name = scanner.nextLine(); // JDK7后的新特性,表达式结果可以是字符串! switch (name){ case "甲": System.out.println("您为事逼但出钱甲方!"); break; case "乙": System.out.println("您为苦逼但挣钱不磕碜的乙方!"); break; default: System.out.println("输入错误!"); } scanner.close(); }}- case标签必须为字符串常量或者字面量
switch(wxpression){ case value : // 语句 break;// 可选 case value : // 语句 break;// 可选 // 你可以有任意数量的case语句 default : //可选 // 语句} -
如果case后不加break,依据case的穿透性,程序会依次输出之后的case
public class SwitchDemo01 { public static void main(String[] args) { // case 穿透 char grade = 'C'; 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("未知等级"); } }} -
4.循环结构
while循环
- while是最基本的循环
while(布尔表达式){ // 循环内容}
- 只要布尔表达式为true,循环就会一直执行下去
- 我们大多数情况是会让循环停止,需要一个让表达式失效的方式来结束循环
- 少部分的情况需要循环一直执行,比如服务器的请求响应监听等。
- 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃
案例一
public class WhileDemo01 { public static void main(String[] args) { // 输出数字1——100 int i = 0; while (i < 100) { i++; System.out.println(i); } }}
案例二
public class WhileDemo02 { public static void main(String[] args) { // 计算1+2+3+。。。。+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(布尔表达式);
- while与do.while的区别
- while先判断后执行,dowhile是先执行后判断
- do while总是保证循环体会被执行至少一次,这是他们的主要区别
demo01
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); }}
demo02
public class DoWhileDemo02 { public static void main(String[] args) { int a = 0; // while 先进行判断,因为a不肯小于0, // 所以while里面的内容不可能执行 while (a < 0) { System.out.println(a); a++; } System.out.println("============================="); // do while是先执行do, // 输出a=0,然后判断a不会小于0,所以不在执行,只输出了0 do { System.out.println(a); a++; } while (a < 0); }}
for 循环
- 虽然所有循环结构都可以用while或者do...while表示,但java提供了另外一种语句——for循环,使一些循环 结构变得简单
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
- for循环执行的次数是在执行前就确定的。
for(初始化;布尔表达式;更新){ // 代码语句}
public class ForDemo01 { public static void main(String[] args) { int a = 1;// 初始化条件 while (a < 100) {// 条件判断 System.out.println(a);// 循环体 a += 2;// 迭代 间隔为2进行迭代 } System.out.println("while循环结束"); // 初始化;条件判断;迭代 // 100.fori 快速生成for循环 for (int i = 1; i < 100; i++) { System.out.println(i); } System.out.println("for循环结束"); }}
案例一:计算从0-100之间的奇数和偶数之和
public class ForDemo02 { public static void main(String[] args) { //案例一:计算从0-100之间的奇数和偶数之和 int oddSum = 0; int evenSum = 0; for (int i = 0; i < 100; i++) { if (i % 2 != 0) {// 这个数和2整除取余,当余数不为0,说明是奇数 oddSum += i;// oddSum = oddSum + i } else {// 为0,则是偶数 evenSum += i; } } System.out.println("奇数的和:" + oddSum); System.out.println("偶数的和:" + evenSum); }}
案例二:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出三个数
public class ForDemo03 { public static void main(String[] args) { // 案例二:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出三个数 for (int i =0;i<=1000;i++){ if (i%5==0 && i!=0){// 能被5整除,且不等于0 System.out.print(i+"\t"); } if (i%(5*3)==0){// 每三个能被5整除的,换一行 System.out.println(); //System.out.println("\n"); } } //println 输出完会换行 //print 输出玩不会换行 }
案例三:打印99乘法表
public class ForDemo04 { public static void main(String[] args) { // 打印99乘法表 //1.先打印第一例 //2.固定的1再用循环包起来 //3.去掉重复项,i<=j //4.调整样式 for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(i + "*" + j + "=" + (j * i) + "\t"); } System.out.println(); } }}
增强for循环
for(声明语句:表达式){ // 代码句子}
-
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数据元素的值相等。
-
表达式:表达式是要访问的数组名,或者是返回值为数据的方法
-
重点是用来循环数组和集合的
public class ForDemo05 { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50};// 定义了一个数组 for (int i = 0; i < 5; i++) { System.out.println(numbers[i]); } System.out.println("========我是分割线========="); // 遍历数组的元素 for (int x : numbers) { System.out.println(x); } }}
break continue
- break在任何循环语句的主体部分,均可用break控制循环的流程。
- break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
- continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
break案例
public class BreakDemo { public static void main(String[] args) { int i = 0; while (i<100){ i++; System.out.println(i); if (i==25){ break; } } }}
continue案例
public class Continue { 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); } // break在任何循环语句的主题部分,均可用break控制循环的流程。 // break用于强行退出循环,不执行循环中的剩余的语句。(break语句也在switch语句中执行) /
/ continue 语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定 }}
- 关于goto关键字
- goto关键字
![]()
- goto关键字

public class LabelDemo { public static void main(String[] args) { // 打印101-150之间所有的质数 // 质数是指在大于1的自然数中,除了1和它本身以外,不再有其他因数的自然数 int count = 0; outer:for (int i = 101;i<150;i++){ for (int j = 2;j<1/2;j++){ if (i%j==0){ continue outer; } } System.out.print(i+" "); } }}
- 联系
// 打印三角形public class TestDemo { public static void main(String[] args) { // 打印三角形 5行 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();// 换行 } }}


浙公网安备 33010602011771号