2021-06-10学习笔记——Java流程控制

1.用户交互Scanner

· 我们可以通过Scanner类来获取用户的输入

· 基本用法:

Scanner s = new Scanner(System.in)

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

package com.dyo.scanner;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

//        System.out.println("使用next方法接收:");
        System.out.println("使用hasNextLine方法接收:");
        //判断用户有没有输入字符
//        if(scanner.hasNext()){
        if(scanner.hasNextLine()){
//            String str = scanner.next();
            String str = scanner.nextLine();
            System.out.println("输出内容为:"+str);
            //next输出hello,nextLine()输出hello world
        }
        //凡是IO流的类如果不关闭会一直占用资源
        scanner.close();
    }
}

· next()

  1. 定要读取到有效字符才可结束输入
  2. 对输入有效字符之前遇到的空白,next()会自动丢弃
  3. 只有输入有效字符才将其输入的空白字符作为分隔符
  4. next()不能得到带有空格的字符串

· nextLine()

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

*scanner进阶使用

package com.dyo.scanner;

import java.util.Scanner;

public class demo3 {
    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;
            sum = sum + x;
        }
        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值为" + (sum/m));

        scanner.close();
    }
}

2.顺序结构

Java的基本结构就是顺序结构

3.选择结构

*if单选择结构

package com.dyo.structure;

import java.util.Scanner;

public class ifdemo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入字符串:");
        String s = scanner.nextLine();

        if(s.equals("Hello")){
            System.out.println("s");
        }
        System.out.println("End");
        scanner.close();
    }
}

  • if双选择结构
package com.dyo.structure;

import java.util.Scanner;

public class ifdemo2 {
    public static void main(String[] args) {
        //考试大于60是及格,小于60是不及格
        Scanner scanner = new Scanner(System.in);

        int score = scanner.nextInt();
        if(score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }
}

  • if多选结构
package com.dyo.structure;

import java.util.Scanner;

public class ifdemo2 {
    public static void main(String[] args) {
        //考试大于60是及格,小于60是不及格
        Scanner scanner = new Scanner(System.in);

        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嵌套结构
  • switch多选择结构
    switch语句中的变量类型可以是:
    • byte、short、int、char
    • 从 JavaSE开始,switch支持字符串类型
    • case标签必须为字符串常量或字面量
package com.dyo.structure;

public class SwitchDemo2 {
    public static void main(String[] args) {
        String name = "小白dyo";
        //字符的本质还是数字
        //表达式可以是字符串
        switch (name){
            case "小白dyo":
                System.out.println("小白dyo");//输出
                break;
            case "dyo":
                System.out.println("dyo");
                break;
            default:
                System.out.println("干啥呢");
        }
    }
}

4.循环结构

  • while循环
  • `package com.dyo.structure;

public class WhileDemo {
public static void main(String[] args) {
// //while循环
// //输出1-100
// int i = 0;
// while(i<100){
// i++;
// System.out.println(i);
// }

    //while(true){
     //等待客户端连接
     //定时检查
     //......
    //
    // }

    //计算1-100的和
    int i = 0;
    int sum = 0;
    while(i<=100){
        sum = sum + i;
        i++;
    }
    System.out.println(sum);
}

}
`

  • do…while循环
    while先判断后执行,do…while先执行后判断
    do…while循环体至少会被执行一次!
package com.dyo.structure;

public class DoWhileDemo {
    public static void main(String[] args) {
        int a = 0;
        while(a<0){
            System.out.println(a);//不输出
        }
        System.out.println("------------------");

        do{
            System.out.println(a);  //0
        }while(a<0);
    }
}

  • for循环
package com.dyo.structure;

public class ForDemo {
    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 = oddSum + i;
            }else{
                evenSum = evenSum +i;
            }
        }
        System.out.println(oddSum+"和"+evenSum);

        //用while或for循环输出1-1000之间能被5整除的数,并每行输出3个
        for (int j = 0; j < 1000; j++) {
            if(j%5==0){
                System.out.print(j+'\t');
            }
            if (j%(5*3)==0){
                System.out.println();
            }
            //println输出会换行
            //print输出不会换行

        }

        //打印九九乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + j*i + '\t');
            }
            System.out.println();
        }
    }
}

  • 在Java5中引入一种主要用于数组的增强型for循环

5.break&continue

——break在任何循环语句的主体部分,均可用break控制循环的流程;break用于强行退出循环,不执行循环中剩下的语句。
——continue用于循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行的判断
——关于goto关键

6.练习


```java
package com.dyo.structure;

public class TestDemo {
    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();
        }

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值