Java流程控制
Java流程控制
用户交互Scanner
Java给我们提供了这样的一个工具类,用以获取用户的输入,来实现程序与人的交互。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入
语法
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取我们一般需要使用hasNext()与hasNextLine()判断是否华友输入的数据
next():
-
一定要读取到有效字符后才可以结束输入
-
对输入有效字符之前遇到的空白,next()方法会自动将其去掉
-
只有输入有效字符后将其后面输入的空白作为分隔符或者结束符
-
next()不能得到带有空格的字符串
nextLine():
- 以Enter为结束符,nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空白
import java.util.Scanner;
public class Next {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
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();
}
}
import java.util.Scanner;
public class NextLine {
public static void main(String[] args) {
Scanner scanner2 = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if (scanner2.hasNextLine()){
String str2 = scanner2.nextLine();
System.out.println("输出的内容为:" + str2);
}
scanner2.close();
}
}
import java.util.Scanner;
//拓展
public class Expansion {
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("输入的不是小数数据!");
}
scanner.close();
}
}
import java.util.Scanner;
public class Practise {
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("你输入了第" + m + "个数据,当前结果sum = " + sum);
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均数是" + (sum/m));
scanner.close();
}
}
顺序结构
Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。书勋结构是最简单的算法结构
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构
public class Sequence {
public static void main(String[] args) {
System.out.println("Hello1");
System.out.println("Hello2");
System.out.println("Hello3");
System.out.println("Hello4");
System.out.println("Hello5");
}
}
选择结构
if单选择结构
我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样的一个过程在程序中用if语句来表示
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
import java.util.Scanner;
public class Single {
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双选择结构
我们需要有两个判断,需要一个双选择结构,所以就有了if-else结构
语法
if(布尔表达式) {
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
import java.util.Scanner;
public class Double {
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多选择结构
在我们面临的问题存在区间多级判断的时候,就需要一个多选择结构来处理这类问题
语法
if(布尔表达式 1){
//如果布尔表达式 1的值为true 执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true 执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true 执行代码
}else{
//如果以上布尔表达式都不为true 执行代码
}
注
- if语句至多有1个 else 语句,else语 句在所有的 else if 语句之后
- if语句可以有若干个 else if 语句,它们必须在 else 语句之前
- 一旦其中一个 else if 语句检测为true,其他的 else if 以及 else 语句都将跳过执行
import java.util.Scanner;
public class Multiple {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60就不及格
Scanner scanner = new Scanner(System.in);
/*
if语句至多有1个 else 语句,else语 句在所有的 else if 语句之后
if语句可以有若干个 else if 语句,它们必须在 else 语句之前
一旦其中一个 else if 语句检测为true,其他的 else if 以及 else 语句都将跳过执行
*/
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("不及格");
}else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
嵌套的if结构
使用嵌套的if…else语句是合法的,可以在另一个if或者else if语句中使用if或者else if语句,可以像if语句一样嵌套else if… else
语法
if(布尔表达式 1){
//如果布尔表达式 1的值为true 执行代码
if(布尔表达式 2){
//如果布尔表达式 2的值为true 执行代码
}
}
switch多选择结构
switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
语法
switch(expression){
case value :
//语句
break;//可选
case value :
//语句
break;//可选
//可以有任意数量的case语句
default;//可选
//语句
}
switch语句中变量可以是
- byte、short、int或者char
- 从Java SE 7 开始,switch 支持字符串 String 类型
- 同时 case 标签必须为支付穿常量或字面量
public class Switch {
public static void main(String[] args) {
//case穿透 switch 匹配一个具体值
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("为止等级");
}
}
}
//字符串也是数字
public class SwitchExpansion {
public static void main(String[] args) {
String name = "zy";
//JDk7的特性,表达式结果可以是字符串
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch(name){
case "zy":
System.out.println("zy");
break;
case "ZY":
System.out.println("ZY");
break;
default:
System.out.println("null");
}
}
}
循环结构
while循环
while是最基本的循环
语法
while(布尔表达式){
//循环内容
}
注
-
只要布尔值为true,循环就会一直执行下去
-
我们大多数情况是会让循环停止下来的,这需要一个让表达式失效的方式来结束循环
-
少部分情况需要循环一直执行,那比如服务器的请求响应监听
-
循环条件一直为true就会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死奔溃
public class While {
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(布尔表达式)
public class Dowhile {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum = sum +i;
i++;
}while (i <= 100);
System.out.println(sum);
}
}
while 和 do-while 的区别
- while先判断后执行,do-while是先执行后判断
- Do-while总是保证循环体会被至少执行一次
public class Distinction {
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循环
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构
for循环执行的次数是在执行前就确定的
语法
for(初始化; 布尔表达式; 更新) {
//代码语句
}
public class ForDemo {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a <= 100){//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("while循环结束");
//初始化 //条件判断 //迭代
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
System.out.println("for循环结束");
/*
关于 for 循环的几点说明
1.最先执行初始化步骤,可以声明一种类型,也可以初始化一个或多个循环控制变量,也可以是空语句
2.然后,检测布尔表达式的值,如果为true,循环体被执行,如果为false,开始执行循环体后的语句
3.执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)
4.再次检查布尔值表达式,循环执行上面的过程
*/
}
}
增强for循环
Java5引入了一种主要用于数组或的增强型for循环
语法格式
for(声明语句 : 表达式){
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class Demo04 {
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用于强行退出循环,不执行循环中剩余的语句。( break 语句也在 switch 语句中使用)
public class Break {
public static void main(String[] args) {
int i = 0;
while (i < 100){
i++;
System.out.println(i);
if (i == 30){
break;
}
}
}
}
continue
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);
}
//continue是跳过本次操作,break是退出整个循环
}
}
continue是跳过本次操作,break是退出整个循环
goto关键字
goto关键字很早就在程序设计语言中出现,尽管goto是Java的一个保留字,但并未在语言中得到正式使用,然而,在 break 和 continue 这两个关键字上,我们仍能看出一些 goto 的影子——带标签的 break 和 continue
- “标签”是指后面跟一个冒号的标识符,例如:label
- 对Java来说唯一用到标签的地方是在循环语句之前,而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于 break 和 continue 关键字常常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方
public class Label {
public static void main(String[] args) {
//打印101-150之间所有的素数
int count = 0;
outer:for (int i = 101; i <= 150; i++) {
for (int j = 2; j < i/2; j++){
if (i % j == 0){
continue outer;
}
}
System.out.print(i + "\t");
}
}
}
练习
public class Practise {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = i; j < 5; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
for (int j = 1; j <= i - 1; j++){
System.out.print("*");
}
System.out.println();
}
}
}
笔记
Java流程控制.md网盘链接
提取码: 7ch6
点击跳转狂神说
P33-P44视频详解Java流程控制