Java流程控制

一、用户交互Scanner

1.Scanner对象

  • 用Scanne类来获取用户的输入。

  • 创建Scanner对象的基本语法:

    Scanner scanner = new Scanner(System.in);

  • 不使用要关闭Scanner类,否则会一直占用资源

2.next 和 nextLine

  • next()

    1. 一定要读取导到有效字符后才可以结束输出。
    2. 对输入有效字符之前遇到的空白,next()方法会将其去掉。
    3. 对输入有效字符之后遇到的空白,next()方法会将其作为分隔符或结束符。
    4. next()方法不能得到带有空格的字符。
  • nextLine()

    1. 以Enter为结束符。
    2. 可以得到空白。

3.其他方法

  1. int数据的输入

    先用hasNextInt()进行验证,再用nextInt()来读取

if (scanner.hasNextInt()){
    i = scanner.nextInt();
    System.out.println("输入的整数为:"+ i );
}else{
    System.out.println("输入的不是整数!");
}
  1. float数据的输入

    先用hasNextFloat()进行验证,再用nextFloat()来读取

if (sc.hasNextFloat()){
    f = sc.nextFloat();
    System.out.println("输入的小数为:"+ f );
}else{
    System.out.println("输入的不是小数!");
}

二、顺序结构

  • 顺序结构是Java最简单的算法结构
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("hello4");//从上到下依次执行
}

三、选择结构

1.if单选择结构

if(布尔表达式){
执行语句
}

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

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equal:判断字符串是否相等
        if (s.equals("Hello")){
            System.out.println(s);
        }
            System.out.println("End");

        scanner.close();
    }

2.if双选择结构

if(布尔表达式){
执行语句
}else{
执行语句
}

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

3.if多选择结构

if(布尔表达式){
执行语句
}else if(布尔表达式){
执行语句
}else{
执行语句
}

        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 >=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 语句至多有1个 else 语句,else 语句在所有 else if 语句之后。
  • if 语句可以有若干个 else if 语句,他们必须在 else 语句之前。
  • 一旦其中一个 else if 语句检测为 true , 其他的 else if 语句和 else 语句都将跳过执行。

4.嵌套的if结构

if(布尔值表达式1{
    if(布尔值表达式2{
    }
}

5.switch多选择结构

switch(expression){
case value:
执行语句
break;(可选)
case value:
执行语句
break;(可选)
default:(可选)
执行语句
}

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

        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("位置等级");
        }
    }
  • switch语句中的变量类型可以是:byte、short、int、char、字符串String类型,同时case标签必须为字符串常量或者字面量。

  • case语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面量。

  • 当变量的值与case的值相等时,执行下一个语句,如果遇到break则跳出switch语句。

  • case不必须要包含break语句。

  • switch语句中可以包含一个default分支,一般在语句的最后一个分支,default在没有case语句的值和变量的值相等时执行,default不需要break语句。

  • case穿透:如果case语句块中没有break语句,匹配成功后,从当前case开始,后续所有case的值都会输出。除非遇到break语句跳出。

四、循环结构

1.while循环

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

  • 大多数情况一般都会让循环停止下来。
public static void main(String[] args) {
    //输出1~100

    int i = 0;
    while (i<100){
        i++;
        System.out.println(i);
    }
  • 死循环:少数情况下会让循环一直进行,不停止,应用于服务器的请求响应监听等。
public static void main(String[] args) {

    while (true){
        //等待客户链接
        //定时检查
        //。。。。。。。。
        //业务中应该尽量避免死循环
    }

2.do…while循环

do{
执行语句
}while(布尔表达式);

  • do…while循环会先执行一次,在根据条件决定是否进行循环。
public static void main(String[] args) {
    //计算1+2+3+...+100=?
    int i = 0;
    int sum = 0;

    do {
        sum = sum + i;
        i++;
    }while (i<=100);

    System.out.println(sum);
}

3.for循环

for(初始化条件;条件判断;更新迭代){
循环体
}

  • for循环的执行次数在执行前就是确定的,for循环是最有效、最灵活的循环结构。

  • for循环最先执行初始化步骤,可以声明一种类型,但是可以初始化一个或多个循环控制变量,也可以是空语句。

//初始化条件;条件判断;更新迭代           //for循环是 最有效 最灵活 的循环结构
for (int i =1;i<=100;i++){
    System.out.println(i);//循环体
}


for (int i = 0; i < 100; i++) {  //100.for   
    
}

//死循环
for (;;){

}

4.循环练习

  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);
    }
  1. 输出1-1000之间能被5整除的数,并且每行输出3个。
public static void main(String[] args) {
    for (int i = 1; i <= 1000; i++) {
        if(i%5==0){
            System.out.print(i+"\t");
        }
        if (i%(3*5)==0){
            System.out.println();
            //System.out.print("\n");
        }
    }
 //println 输出完会换行
 //print 输出完不会换行
}
  1. 打印九九乘法表。
public static void main(String[] args) {
    //int j = 9;
    for (int j = 1; j <= 9; j++) {
        for (int i = 1; i <= j; i++) {
            System.out.print(i+"*"+j+"="+(j*i)+"\t");
        }
        System.out.println();
    }
}
  1. 打印三角形。(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();
    }
}

5.增强for循环

for(声明语句:表达式){
执行代码
}

int[] numbers = {10,20,30,40,50};//定义了一个数组

for (int i = 0; i < 5; i++) {
    System.out.println(numbers[i]);
}

System.out.println("======================");

//增强for循环   主要用于数组或集合的增强型for循环    遍历数组的元素
for (int x:numbers){
    System.out.println(x);
}

五、break和continue

1.break关键字

break语句用来强行跳出该循环,不执行循环中剩余的语句,继续执行该循环下面的语句,主要用在循环语句和switch语句中。

public static void main(String[] args) {
    int i = 0;
    while (i<100){
        i++;
        System.out.println(i);
        if (i==30){
            break;//输出30之后,不会继续输出,跳出循环
        }
    }
    System.out.println(132);
}

2.continue关键字

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

3.goto关键字 标签(了解)

标签是后边跟着一个冒号的标识符, label:

public static void main(String[] args) {
    //打印101-150之间所有的质数
    int count = 0;
    //不建议使用!!!
    outer:for (int i =101;i<150;i++){
        for (int j = 3; j < i/2; j++){
            if (i % j == 0){
                continue outer;//了解标签
            }
        }
        System.out.print(i+" ");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值