【阶段一】java之流程控制

java中的流程控制是什么呢?
if、if…else、while、do…while、for、switch…case等

Scanner

1、Scanner对象
之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我 们可以获取用户的输入。java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入。

下面是创建 Scanner 对象的基本语法:

Scanner scanner = new Scanner(System.in)

2、next & nextLine

public static void ScannerDome(String[] args) {
    //创建一个扫描器对象,用于接收键盘数据
    Scanner scanner = new Scanner(System.in);
    //next方式接收字符串
    System.out.println("Next方式接收:");
    //判断用户还有没有输入字符
    if (scanner.hasNext()) {
        String str = scanner.next();
        System.out.println("输入内容:" + str);
    }
    //清理
    scanner.close();
}

测试数据:Hello World!
结果:只输出了Hello。

public static void ScannerDome(String[] args) {
    Scanner scan = new Scanner(System.in);
    // 从键盘接收数据
    // nextLine方式接收字符串
    System.out.println("nextLine方式接收:");
    // 判断是否还有输入
    if (scan.hasNextLine()) {
        String str2 = scan.nextLine();
        System.out.println("输入内容:" + str2);
    }
    //清理
    scan.close();
}

测试数据:Hello World!
结果:输出了Hello World!

两者区别:
next():
一定要读取到有效字符后才可以结束输入。
对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。

nextLine():
以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
可以获得空白

3、其他方法
如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    // 从键盘接收数据
    int i = 0;
    float f = 0.0f;
    System.out.print("输入整数:");
    if (scan.hasNextInt()) {
        // 判断输入的是否是整数
        i = scan.nextInt();
        // 接收整数
        System.out.println("整数数据:" + i);
    } else {
        // 输入错误的信息
        System.out.println("输入的不是整数!");
    }
    System.out.print("输入小数:");
    if (scan.hasNextFloat()) {
        // 判断输入的是否是小数
        f = scan.nextFloat();
        // 接收小数
        System.out.println("小数数据:" + f);
    } else {
        // 输入错误的信息
        System.out.println("输入的不是小数!");
    }
    scan.close();
}

具体Scanner类都有什么方法,可查看其中的源码。

顺序结构

JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
顺序结构是最简单的算法结构。

顺序结构在程序流程图中的体现就是用流程线将程序框自上而地连接起来,按顺序执行算法步骤。

public static void main(String[] args) {
    System.out.println("1");
    System.out.println("2");
    System.out.println("3");
    System.out.println("4");
    System.out.println("5");
}
//按照自上而下的顺序执行!依次输出。

选择结构

1、if单选择结构

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
}

意义:if语句对条件表达式进行一次测试,若测试为真,则执行下面的语句,否则跳过该语句。
【例子】比如我们来接收一个用户输入,判断输入的是否为Hello字符串:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    //接收用户输入
    System.out.print("请输入内容:");
    String s = scanner.nextLine();
    
    if (s.equals("Hello")){
        System.out.println("输入的是:"+s);
    }
    
    System.out.println("End");
    scanner.close();
}

equals方法是用来进行字符串的比较的,之后会详解,这里大家只需要知道他是用来比较字符串是否 一致的即可!和==是有区别的。

2、if双选择结构

if(布尔表达式){
    //如果布尔表达式的值为true
    }else{
    //如果布尔表达式的值为false
}

意义:当条件表达式为真时,执行语句块1,否则,执行语句块2。也就是else部分。

【例子】考试分数大于60就是及格,小于60分就不及格。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("请输入成绩:");
    int score = scanner.nextInt();
    
    if (score>60){
        System.out.println("及格");
    }else {
        System.out.println("不及格");
    }
    
    scanner.close();
}

3、if多选择结构

if(布尔表达式 1){
    //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
    //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
    //如果布尔表达式 3的值为true执行代码
}else {
    //如果以上布尔表达式都不为true执行代码
}

if 语句后面可以跟 else if…else 语句,这种语句可以检测到多种可能的情况。

使用 if,else if,else 语句的时候,需要注意下面几点:
· if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
· if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
· 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。

【例子】我们来改造一下上面的成绩案例,学校根据分数区间分为ABCD四个等级!

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("请输入成绩:");
    int score = scanner.nextInt();
    
    if (score==100){
        System.out.println("恭喜满分");
    }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();
}

4、嵌套的if结构

if(布尔表达式 1){
    如果布尔表达式 1的值为true执行代码
    if(布尔表达式 2){
        如果布尔表达式 2的值为true执行代码
    }
}

5、switch多选择结构

switch(expression){
    case value :
        //语句
        break; //可选
    case value :
        //语句
        break; //可选
    //你可以有任意数量的case语句
    default : //可选
        //语句
}

switch case 语句有如下规则:

· switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标 签必须为字符串常量或字面量。
· switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
· case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
· 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
· 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
· switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

public static void main(String[] args) {
    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 'F':
            System.out.println("你需要再努力努力");
            break;
        default:
            System.out.println("未知等级");
    }
    System.out.println("你的等级是 " + grade);
}

如果 case 语句块中没有 break 语句时,匹配成功后,从当前 case 开始,后续所有 case 的值都会输 出。如果后续的 case 语句块有 break 语句则会跳出判断。

public static void main(String[] args) {
    int i = 1;
    switch (i) {
        case 0:
            System.out.println("0");
        case 1:
            System.out.println("1");
        case 2:
            System.out.println("2");
        case 3:
            System.out.println("3");
            break;
        default:
            System.out.println("default");
    }
}

输出:1,2,3。

case 后边的可以是字符串的表达形式

public static void main(String[] args) {
    String name = "你好";

    switch (name) {
        //JDK7的新特性,表达式结果可以是字符串!!!
        case "你好":
            System.out.println("你好");
            break;
        case "我好":
            System.out.println("我好");
            break;
        default:
            System.out.println("啥都不好");
            break;
    }
}

循环结构

Java中有三种主要的循环结构:

· while 循环
· do…while 循环
· for 循环

1、while 循环

while( 布尔表达式 ) {
    //循环内容
}

方式有:循环内部控制,外部设立标志位!等

public static void main(String[] args) {
    int i = 0;
    //i小于100就会一直循环
    while (i<100){
        i++;
        System.out.println(i);
    }
}

少部分情况需要循环一直执行,比如服务器的请求响应监听等。

public static void main(String[] args) {
    while (true){
        //等待客户端连接
        //定时检查
        //......
    }
}

循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影 响程序性能或者造成程序卡死奔溃!
【案例:计算1+2+3+…+100=?】

public static void main(String[] args) {
    int i = 0;
    int sum = 0;
    while (i <= 100) {
        sum = sum+i;
        i++;
    }
    System.out.println("Sum= " + sum);
}

2、do…while 循环
对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少 执行一次。
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

do {
    //代码语句
}while(布尔表达式);

public static void main(String[] args) {
    int i = 0;
    int sum = 0;
    do {
        sum = sum+i;
        i++;
    }while (i <= 100);
    System.out.println("Sum= " + sum);
}

执行结果当然是一样的!

While和do-While的区别:
while先判断后执行。dowhile是先执行后判断!
Do…while总是保证循环体会被至少执行一次!这是他们的主要差别。

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);
}

3、For循环
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
for循环执行的次数是在执行前就确定的。语法格式如下:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

关于 for 循环有以下几点说明:

· 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
· 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
· 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减。
· 再次检测布尔表达式。循环执行上面的过程。
【例子:while和for输出】

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("while循环结束!");
}

我们发现,for循环在知道循环次数的情况下,简化了代码,提高了可读性。我们平时用到的最多的也是 我们的for循环!

break & continue

1、break 关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。

break 跳出最里层的循环,并且继续执行该循环下面的语句。

【例子:跳出循环】

public static void main(String[] args) {
    int i = 0;
    while (i < 100) {
        i++;
        System.out.println(i);
        if (i == 30) {
            break;
        }
    }
}

2、continue 关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

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);
    }
}

3、两者区别
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)

continue 语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

练习

【练习1:计算0到100之间的奇数和偶数的和】

public static void main(String[] args) {
    int oddSum = 0; //用来保存奇数的和
    int evenSum = 0; //用来存放偶数的和
    for (int i = 0; i <= 100; i++) {
        if (i % 2 != 0) {
            oddSum += i;
        } else {
            evenSum += i;
        }
    }
    System.out.println("奇数的和:" + oddSum);
    System.out.println("偶数的和:" + evenSum);
}

【练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个】

public static void main(String[] args) {
    for (int j = 1; j <= 1000; j++) {
        if (j % 5 == 0) {
            System.out.print(j + "\t");
        }
        if (j % (5 * 3) == 0) {
            System.out.println();
        }
    }
}

【练习3:打印九九乘法表】

public static void main(String[] args) {
   for (int i = 1; i <= 9 ; i++) {
   		for (int j = 1; j <= i; j++) {
        	System.out.print(j + "*" + i + "=" + (i * j)+ "\t");
    	}
    	System.out.println();
	}
}

感谢各位大大的观看!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值