Java控制流程

1. 用户交互Scanner

之前所提到的基本语法基本没有实现程序和人的交互,但是Java有提供这样的一个工具,可以获取用户的输入。java.util.Scanner是Java5的新特征,可以通过Scanner类来获取用户的输入。

基本的语法:

Scanner s = new Scanner(System.in);

通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。在这里插入图片描述

package com.hjb.scanner;

import java.util.Scanner;

public class Demo01 {
    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();
    }
}
package com.hjb.scanner;
import java.util.Scanner;
public class Demo02 {
    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();
    }
}
package com.hjb.scanner;
import java.util.Scanner;
public class Demo03 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数据: ");

        String str = scanner.nextLine();

        System.out.println("输入的内容是:"+str);

        scanner.close();
    }
}

2. Scanner进阶使用

package com.hjb.scanner;

import java.util.Scanner;

public class Demo04 {
    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();
    }
}
package com.hjb.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 x = scanner.nextDouble();
            m = m + 1;//m++
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
        }
        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值为" + (sum / m));
        scanner.close();
    }
}

3. 顺序结构

JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句话一句话执行。
顺序结构是最简单的算法结构。顺序结构
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

package com.hjb.struct;
public class ShunXuDemo {
    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");
    }
}

4. if选择结构

4.1 if单选择结构

我们很多时候需要去判断一个东西是否可行,然后才去执行,这样一个过程在程序中用if语句来表示。if单选择结构

package com.hjb.struct;
import java.util.Scanner;
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();
    }
}

4.2 if双选择结构

假如现在有个需求,公司要收购一个软件,成功了,给人支付100万元,失败了,自己找人开发。这样的需求只用一个if是搞不定的,需要有两个判断,需要一个双选择的结构,所以就有了if-else的语句。if双选择结构

package com.hjb.struct;
import java.util.Scanner;
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();
    }
}

4.3 if多选择结构

针对上面的代码,想要实现区间多级判断,比如90-100是A,80-90是B等,很多的时候选择不仅仅只有两个,所以需要一个多选择结构来处理这类的问题。if多选择结构

package com.hjb.struct;
import java.util.Scanner;
public class IfDemo03 {
    public static void main(String[] args) {
        //考试进行分级判断
        Scanner scanner = new Scanner(System.in);
        /*
        if语句至多有一个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("恭喜满分");
        }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.4 if嵌套结构

使用嵌套的if…else语句是合法的。也就是说可以在另一个if或者else if语句中使用if或者else if语句。可以像if语句一样嵌套else if…else。if嵌套结构

5. Switch多选择结构

多选择结构还有一个实现方式就是switch case语句。switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch case语句中的变量类型可以是:

  • byte、short、int或者char
  • 从Java SE 7 开始,switch支持字符串String了
  • 同时case标签必须为字符串常量或者字面量switch多选择结构
package com.hjb.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case 穿透    //switch匹配一个具体的值
        char grade = 'F';

        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("未知等级");
        }
    }
}
package com.hjb.struct;
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "黄季兵";
        //JDK7的新特性,表达式结果可以是字符串!!!
        //字符的本质还是数字
        //反编译    java---class(字节码文件)---反编译(IDEA)
        switch(name){
            case "冰冷依燃":
                System.out.println("冰冷依燃");
                break;
            case "Bingo":
                System.out.println("Bingo");
                break;
            case "黄季兵":
                System.out.println("黄季兵");
                break;
            default:
                System.out.println("都不对");
        }
    }
}

接下来了解一下反编译,去什么地方看呢?打开IDEA的Project Structure,打开Project compiler output,如E:\JAVA\code\JavaSE\out,我们找到对应的class文件,这个class是一堆乱码,是看不懂的,但是可以通过IDEA查看,直接复制这个是复制不过去的,需要从IDEA里面用open in explorer,将class文件复制过去。反编译
其实就是每一个中文都有自己的哈希值,这就是反编译:java—class(字节码文件)—反编译(IDEA)。

6. While循环结构

循环主要有while,do…while,for,在Java5中引入了一种主要用于数组的增强型for循环。while循环是最基本的循环,它的结构为:

while(布尔表达式){
    //循环的内容
}
  • 只要布尔表达式为true,循环就会一直执行下去。
  • 我们大多数的情况会让循环停止下来,我们需要一个让表达式失效的方式来结束循环。
  • 少部分情况需要一直循环运行,比如服务器的请求响应监听等。
  • 循环条件一直为true就会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
package com.hjb.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1-100
        int i = 0;
        while(i<100){
           i++;
           System.out.println(i);
        }
    }
}
package com.hjb.struct;
public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
        }
    }
}
package com.hjb.struct;
public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+4+···+100=?
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum +i;
            i++;
        }
        System.out.println(sum);
    }
}

7. DoWhile循环结构

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

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

While和do…while的区别:

  • while先判断后执行,do…while是先执行后判断。
  • Do…while总是保证循环体会被至少执行一次,这也是它们的主要差别。
package com.hjb.struct;
public class DoWhileDemo01 {
    public static void main(String[] args) {
        //计算1+2+3+4+···+100=?
        int i = 0;
        int sum = 0;
        do{
            sum = sum +i;
            i++;
        }while(i<=100);
        System.out.println(sum);
    }
}
package com.hjb.struct;
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);
    }
}

8. For循环结构

虽然所有的循环结构都可以使用do…while循环和while循环表示,但是Java提供了另一种的语句:for循环,使得一些循环的结构变得简单。f**or循环语句是支持迭代的一种通用结构,也是最有效、最灵活的循环结构。**for循环执行的次数是在执行前就确定的。语法格式如下:

for(初始化;布尔表达式;更新){
    //代码语句
}
package com.hjb.struct;
public class ForDemo01 {
    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=i+2){//可以使用100.for
            System.out.println(i);
        }
        System.out.println("for循环结束");
        /*关于for循环的几点说明:
        最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
        然后检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句。
        执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
        再次检测布尔表达式,循环执行上面的过程。
        for(; ; ;){
           //这是一个死循环结构
        }
         */
    }
}
package com.hjb.struct;
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){//奇数
                oddSum+=i;
            }else{//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}
package com.hjb.struct;
public class ForDemo03 {
    public static void main(String[] args) {
        //练习二:用while或者是for循环输出1-1000之间能被5整除的数,并且每行输出3个
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");//水平制表符(相当于tab,缩进),\t是补全当前字符串长度到8的整数倍,最少1个最多8个空格,补多少要看你\t前字符串长度
            }
            if (i%(5*3)==0){
                //System.out.println();
                System.out.println("\n");
            }
        }
        //println 输出完会换行
        //print 输出完不会换行
    }
}
package com.hjb.struct;
public class ForDemo04 {
    public static void main(String[] args) {
        //练习三:打印九九乘法表
        /*
        1*1=1
        2*1=2  2*2=4
        3*1=3  3*2=6  3*3=9
        4*1=4  4*2=8  4*3=12 4*4=16
        5*1=5  5*2=10 5*3=15 5*4=20 5*5=25
        6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
        7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
        8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
        9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
         */
        for(int i = 1; i <= 9; i++) {
            for(int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j+"="+i*j+"\t") ;
            }
            System.out.println();
        }
    }
}

9. 增强for循环结构

这边先做了解,之后数组的时候会重点使用,Java5引入了一种主要用于数组或者集合的增强型for循环,语法格式如下:

for(声明语句;表达式)
{
    //代码语句
}
package com.hjb.struct;
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);
        }
    }
}

10. break、continue和goto

break语句

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

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

        }
    }
}

continue语句

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

package com.hjb.struct;
public class ContinueDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if (i%10==0){
            System.out.println();
            continue;
            }
            System.out.println(i);
        }
    }
}

goto关键字

  • goto关键字很早就在程序设计语言中出现。尽管goto仍然是java的一个保留字,但并未在语言中得到正式应用;Java没有goto。然而在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子——带标签的break和continue。
  • “标签”是指后面跟一个冒号的标识符,例如label:
  • 对java来说唯一用到标签的地方是在循环语句之前,而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只能中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
package com.hjb.struct;
public class LabelDemo {
    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.println(i+"\t");
        }
    }
}

11. 打印三角形及Debug

package com.hjb.struct;
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();
        }
    }
}

关于debug,按瓢虫按钮即可进行一步步的调试即可。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

binglengyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值