新手初学Java流程控制

 更新时间:2021年7月7日 15:00  点击:2333
目录

Java流程控制

用户交互Scanner

  • java.util.Scanner是Java5的新特征,可以通过Scanner类来获取用户的输入

  • 基本语法

Scanner sc = new Scanner(System.in);

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

next()

1、一定要读取到有效字符后才可以结束输入。

2、对输入有效字符之前遇到的空白,next()方法会自动将其去掉。

3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。

4、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();//如果输入了 hello world,则输出的内容只有hello
            System.out.println("输入的内容为:"+str);
        }
        //属于IO流(输入输出流)的类需要关闭,如果不关闭会占用资源
        scanner.close();
    }

nextLine()

1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。

2、可以获得空白之后的字符。

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收");
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();//输入 hello world可以在控制台完整输出
            System.out.println("输入的内容为:"+str);
        }
        scanner.close();
    }

整数类型,浮点数类型也有相应的用法

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数");
        if (scanner.hasNextInt()){
            int a = scanner.nextInt();
            System.out.println("你输入的整数:"+a);
        }else {
            System.out.println("你输入的不是整数");
        }
        System.out.println("请输入一个小数");
        if (scanner.hasNextFloat()){
            float b = scanner.nextFloat();
            System.out.println("你输入的小数:"+b);
        }else {
            System.out.println("你输入的不是小数");
        }
        scanner.close();
    }

选择结构

if单选择结构

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

语法

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

例:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个内容");
        String str = scanner.nextLine();
        //如果你输入的内容是Hello,则会打印出来
        if(str.equals("Hello")){
            System.out.println(str);
        }
        scanner.close();
    }

if双选择结构

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

语法

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

例:

public static void main(String[] args) {
        //如果输入成绩大于等于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多选择结构

我们发现刚才的代码不符合实际情况,真实的情况还可能存在ABCD,存在区间多级判断。比如90-100就是A,80-90 就是B..等等,在生活中我们很多时候的选择也不仅仅只有两个,所以需要一个多选择结构来处理这类问题!

语法

if(布尔表达式1){
    //如果布尔表达式1为true将执行的语句
}else if(布尔表达式2){
	//如果布尔表达式2为true将执行的语句
}else if(布尔表达式3){
	//如果布尔表达式3为true将执行的语句
}else{
	//如果以上布尔表达式都不为true时执行
}

例:

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("A");
        }else if(score<90 && score>=70){
            System.out.println("B");
        }else if(score<70 && score>=60){
            System.out.println("C");
        }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将执行的代码
    }
}

例:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个1到100之间的整数");
        int i = scanner.nextInt();
        if(i<=100){
            if(i>=1){
                System.out.println("合法数字:"+i);
            }else {
                System.out.println("不合法");
            }
        }else{
            System.out.println("不合法");
        }
        scanner.close();
    }

Switch多选择结构

多选择结构还有一个实现方式就是switch case语句。

switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

语法:

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

switch语句中的变量类型可以是:

  • byte、short、int或者char。

  • 从Java SE7开始switch支持字符串String类型了

  • 同时case标签必须为字符串常量或字面量。

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = scanner.nextLine();
        switch (name){
            case "李四":
                System.out.println(name);
                break;
            case "张三":
                System.out.println(name);
                break;
            default:
                System.out.println("你的名字:"+name);
        }
    }

while循环

while是最基本的循环,只要布尔表达式为true,循环就会一直执行下去。它的结构为:

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

例:使用while循环计算1+2+3+...+100的和

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循环是先执行再判断,while是先判断再执行

语法:

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

例:do while和while的区别

public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("========================");
        //只有分割线下面的out语句输出了
        do{
            System.out.println(a);
            a++;
        }while (a<0);
    }

For循环

for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

for循环执行的次数是在执行前就确定的。语法格式如下:

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

例1:计算0到100之间的奇数和偶数的和

public static void main(String[] args) {
        //计算0到100之间的奇数和偶数的和
        int odd = 0;//odd作为奇数总和
        int even = 0;//even作为偶数总和
        for (int i = 0; i < 100; i++) {
            if(i%2==0){
                even+=i;
            }else {
                odd+=i;
            }
        }
        System.out.println("奇数总和:"+odd);//输出2500
        System.out.println("偶数总和:"+even);//输出2450
    }

例2:用for循环输出1-1000之间能被5整除的数,并且每行输出3个

public static void main(String[] args) {
        //用for循环输出1-1000之间能被5整除的数,并且每行输出3个
        for (int i = 1; i < 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(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();
        }
    }

增强for循环

Java5 引入了一种主要用于数组或集合的增强型for循环。增强for循环语法格式如下:

for(声明句式 : 表达式){
	//代码
}
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

注意:

1.在使用增强型for循环不支持遍历时删除元素

2.使用增强型for循环时,对遍历的集合需要做null判断,不然可能引发空指针异常。

public static void main(String[] args) {
        //定义一个数组array
        int [] array = {1,2,3,4,5,6};
        for(int i : array){
            System.out.print(i);//输出123456
        }
        System.out.println();
        //for(int i : array)相当于下面的for循环
        for(int i=0;i<array.length;++i){
            int j = array[i];
            System.out.print(j);//输出123456
        }
    }

break和continue

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

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

例:

public static void main(String[] args) {
        int i = 0;
        int j = 0;
        while (i<100){
            i++;
            if(i==30){
                break;
            }
            System.out.println(i);//输出1-29就停止了
        }
        System.out.println("------------------------");
        while (j<100){
            j++;
            if(j%2==0){
                continue;
            }
            System.out.println(j);//输出了100以内的奇数
        }
    }

练习:用循环打印5行的正三角形和倒三角形

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();
        }
    }
public static void main(String[] args) {
        //倒三角形
        int n=5;
        for(int i=n;i>=1;i--){
            for(int j=1;j<=n-i;j++){
                System.out.print(" ");
            }
            for(int j=i*2-1;j>=1;j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }

[!--infotagslink--]

相关文章