JAVA学习 Day04:JAVA流程控制

一、Scanner对象

package com.sun.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.sun.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()){
            //使用nextLine方式接收
            String str =scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }
}

在这里插入图片描述

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

二、顺序结构

顺序结构是任何一个算法都离不开的一种基本算法结构

三、选择结构

1.if单选择结构

if(布尔表达式){
//如果为true将执行的语句
}
package com.sun.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();
    }
}

2.if双选择结构

package com.sun.struct;

import java.util.Scanner;

public class ifDemo02 {
    public static void main(String[] args) {
        //公司收购一个软件,成功了给人家一百万,失败了自己找人开发
        Scanner scanner = new Scanner(System.in);
        System.out.println("是否收购成功:");
        String acquisition = scanner.nextLine();
        if(acquisition.equals("成功")){
            System.out.println("收购成功了,需要支付给人家一百万");
        }else{
            System.out.println("收购失败,需要自己找人开发了");
        }

        scanner.close();
    }
}

3.if多选择结构

package com.sun.struct;

import java.util.Scanner;
import java.util.SortedMap;

public class ifDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //考试分数100是满分,小于100大于80分优秀,小于80大于60及格,低于60是不及格
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if(score==100){
            System.out.println("恭喜获得满分,成绩等级为A+");
        }else if (score>=90 && score<100){
            System.out.println("成绩等级为A");
        }else if (score>=80 && score<90){
            System.out.println("成绩等级为B");
        }else if (score>=60 && score<80){
            System.out.println("成绩等级为C");
        }else if (score>=0 && score<60){
            System.out.println("成绩等级为D,未及格!");
        }else{
            System.out.println("输入的成绩不合法");
        }


        scanner.close();
    }
}

4.嵌套的if结构
在这里插入图片描述
5.switch多选择结构
在这里插入图片描述

package com.sun.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透
        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可以支持字符串了

package com.sun.struct;

public class SwitchDenmo02 {
    public static void main(String[] args) {
        String name = "学··";

        switch (name){
            case"学代码":
                System.out.println("学代码");
                break;
            case"学习":
                System.out.println("学习");
                break;
            default:
                System.out.println("干啥呢?");
        }
    }
}

6.while循环(先判断后执行,不满足条件一次都不执行)
while(布尔表达式){
//循环内容
}

  • 只要表达式为true,循环就会一直执行下去
  • 我们大多数情况下是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
package com.sun.struct;

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

7.do while 循环(即使不满足条件,也至少执行一次)
先执行,后判断
do{
//代码语句
}while(布尔表达式)

package com.sun.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0 ;

        do {
            sum = sum + i;
            i ++;
        }while(i<=100);
        System.out.println(sum);
    }
}

package com.sun.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循环
for(初始化;布尔表达式;更新){
//代码语句
}

  • for循环是支持迭代的一种通用结构,是最有效,最灵活的循环结构
package com.sun.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++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}

package com.sun.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.sun.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                //System.out.println();
                System.out.println("\n");
            }
        }
        System.out.println("\n=======================================");
        //while循环
        int a = 0 ;
        while (a<=1000){
            if (a%5==0){
                System.out.print(a+"\t");
            }
            if (a%(5*3)==0){
                System.out.println();
            }
            a++;
        }

        }
    }


package com.sun.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        //打印九九乘法表
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print (i + "*" + j + "=" + (j*i) + "\t" );
                if (j<=i){
                    System.out.println();
                }
            }

        }

    }
}

9.增强for循环

package com.sun.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

  • break 用于强行退出循环,不执行循环中剩余语句
  • break也可以再switch中使用
  • continue 用于终止某次循环,即跳过循环体中尚未执行的语句,接着执行下一次是否执行循环的判定
package com.sun.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.print(i+"\t");
        }
    }
}

11.练习

package com.sun.struct;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值