Java流程控制:顺序结构、选择结构、循环结构

1.用户交互Scanner

tu1
使用next方式接收

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

使用nextLine方式接收

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

        System.out.println("请输入内容:");

        //使用nextLine方式接收
        String str = scanner.nextLine();

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

        //关闭
        scanner.close();
    }
}

区别
tu2
练习

public class Demo03 {
    public static void main(String[] args) {
        //输入多个数字,求和、求平均值,每输入一个数字按回车确定,当输入数据非数字时结束输入并输出执行结果
        Scanner scanner = new Scanner(System.in);

        //定义变量sum用于存储数据和
        double sum = 0;
        //定义变量i记录数据个数
        int i = 0;

        System.out.println("请输入数据:");
        //通过循环判断是否还有输入,每一次循环进行求和和统计
        while (scanner.hasNextDouble()){
            //接收数据存放于x中
            double x = scanner.nextDouble();
            sum += x;
            i++;
            System.out.println("输入了第"+i+"个数据"+x);
        }
        System.out.println(i+"个数据之和为:"+sum);
        System.out.println(i+"个数据平均值为:"+(sum/i));
        scanner.close();
    }
}

2.顺序结构

tu3

3.选择结构

if单选择结构
tu4
示例

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("你好!")){
            //若值不相等,则不执行内部语句
            System.out.println(s);
        }
        
        System.out.println("结束!");
        scanner.close();
    }
}

if双选择结构
tu5
示例

public class IfDemo02 {
    public static void main(String[] args) {
        //判断成绩是否合格
        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多选择结构
tu6
示例

public class IfDemo03 {
    public static void main(String[] args) {
        //判断成绩等级:A:90~100  B:80~90  C:60~80 D:<60
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:0~100");
        int score = scanner.nextInt();

        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<60){
            System.out.println("成绩等级:D");
        }else {
            System.out.println("成绩输入不正确!");
        }

        scanner.close();
    }
}

注意
tu8

嵌套if结构
tu7
示例

public class IfDemo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");

        int score = scanner.nextInt();

        if(score>=50){
            if(score>=50 && score<60){
                System.out.println("成绩不合格!");
            }else {
                System.out.println("成绩合格!");
            }
        }else {
            System.out.println("成绩不足50!重修!");
        }
        scanner.close();
    }
}

switch多选择结构
tu9
示例

public class SwitchDemo01 {
    public static void main(String[] args) {
    	//jdk7以后支持字符串
        String grade = "A";
		//switch匹配一个具体的值
        switch (grade){
        	//case穿透(如果没有break,则之后的输出语句都会执行)
            case "A":  
                System.out.println("你很优秀!");
                break;
            case "B":
                System.out.println("再接再厉!");
                break;
            case "C":
                System.out.println("继续努力!");
                break;
            case "D":
                System.out.println("没有及格!");
                break;
            default:
                System.out.println("未知等级!");
        }
    }
}

4.循环结构

while循环
tu10
示例

public class WhileDemo01 {
    public static void main(String[] args) {
        //计算1~100所有数的和
        int sum = 0;
        int i = 0;
        while (i<=100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

do…while循环
tu11
示例

public class DoWhileDemo01 {
    public static void main(String[] args) {
        //while与do…while的区别
        int i = 0;
        while (i<0){  //先判断再执行,由于不满足条件,不执行循环体
            System.out.println(i);
            i++;
        }
        System.out.println("==============");
        do{  //先执行再判断,至少执行一次循环体
            System.out.println(i);
            i++;
        }while (i<0);
    }
}

for循环
在这里插入图片描述
zhuyishuoming
示例

public class ForDemo01 {
    public static void main(String[] args) {
        //练习1:计算0-100之间奇数、偶数的和
        int oddSum = 0;  //奇数之和
        int evenSum = 0; //偶数之和

        for (int i = 0; i <= 100; i++) {
            if (i%2 == 0){
                evenSum += i;
            }else {
                oddSum += i;
            }
        }
        System.out.println("奇数之和:"+oddSum);
        System.out.println("偶数之和:"+evenSum);
    }
}
public class ForDemo02 {
    public static void main(String[] args) {
        //练习2:输出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.print("\n");
            }
        }
    }
}
public class ForDemo03 {
    public static void main(String[] args) {
        //练习3:打印九九乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {  //当j大于i后跳出循环,进行下一行计算,去掉重复项
                System.out.print(j+"x"+i+"="+(i*j)+"\t");  //一行一行的打印
            }
            System.out.println(); //打印完一行换行
        }
    }
}

增强型for循环
tu13

public class ForDemo04 {
    public static void main(String[] args) {
        int numbers[] = {10, 20, 30, 40, 50};  //定义一个数组numbers

        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        
        System.out.println("==============");
        
        for (int x:numbers){  //增强for循环
            System.out.println(x);
        }
    }
}

5.break & continue

在这里插入图片描述

public class BreakContinueDemo01 {
    public static void main(String[] args) {
        //break
        int i = 0;
        while (i<100){
            i++;
            if(i%10 == 0){  //if单选择
                System.out.println();
                break;   //直接结束循环
            }
            System.out.print(i+"\t");
        }
        
        System.out.println("==============");
        
        //continue
        int j = 0;
        while (j<100){
            j++;
            if (j%10==0){
                System.out.println();
                continue;  //结束本次循环,重头开始执行新一次的循环
            }
            System.out.print(j+"\t");
        }
    }
}

带标签的continue

public class BreakContinueDemo02 {
    public static void main(String[] args) {
        //输出101-150之间的所有质数(只能被1和它本身整除)

        int num = 0;
		//一般不使用,只作了解
        outer:for (int i = 101; i < 150; i++) {
            for(int j = 2; j < i/2 ; j++){
                if(i%j == 0){
                    continue outer;  //跳转到标签outer
                }
            }
            System.out.print(i+"\t");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值