流程控制

Scanner

Scanner scanner = new Scanner(System.in);

public class Demo03 {
    public static void main(String[] args) {
        int i = 0;
        float f = 0.0f;

        Scanner scanner = new Scanner(System.in);

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

输入输出流,一定记得关闭资源

顺序结构

依次运行

IF

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

        System.out.println("请输入分数:");
        int i = scanner.nextInt();


        if (i==100){
            System.out.println("恭喜你,满分");
        }else if (i<100&&i>=90){
            System.out.println("A");
        }else if (i<90&&i>=80){
            System.out.println("B");
        }else if (i<80&&i>=70){
            System.out.println("C");
        }else if (i<70&&i>=60){
            System.out.println("D");
        }else if (i<60&&i>=0){
            System.out.println("不及格");
        }else {
            System.out.println("输入不合法");
        }



        scanner.close();
    }
}

switch

public class SwitchDemo01 {
    public static void main(String[] args) {
        /**
         * 编译:高级语言转换成低级语言
         * 高级语言:程序员看得懂的语言(java,python编程语言)
         * 低级语言:计算机看得懂得语言(机器语言和汇编语言)
         *
         * 编译过程:
         * .java --> .class --> 机器语言
         *
         * Java反编译:
         * .class --> .java
         */

        String name = "zzl;

        switch (name){
            case "zl:
                System.out.println("zl);
//                break;
            case "zzl:
                System.out.println("zzl");
//                break;
            case "pm":
                System.out.println("pm");
                break;
            default:
                System.out.println("不合法");
        }
    }
}

JDK7新特性:switch允许String类型作为参数

while

public class WhileDemo02 {
    public static void main(String[] args) {
        //计算1-100的和
        int i = 0;
        int sum = 0;
        while (i<=100){

            sum=sum+i;
            i++;
            System.out.println(sum);
        }
    }
}

do while

与while相比,do while至少循环一次

for

重要
for(初始化;布尔表达式;更新){}

for(;;){} // 死循环的表达式
public class ForDemo02 {
    public static void main(String[] args) {
        //循环输出1-1000之间能被5整除的数,并且每行输出3个

        for (int i = 1; i <= 1000; i++) {
            if (i%5 == 0){
//                System.out.print(i+" ");
                System.out.print(i+"\t");
            }
            if (i%(5*3) == 0){//3个数
                System.out.println();
            }
        }
        System.out.println();
    }
}

九九乘法表


        //打印九九乘法表
       //一列一列的处理
 /*       for (int j = 1; j <= 9; j++) {
            System.out.println(1+"*"+j+"="+(1*j)+"\t");
        }
*/
        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();
        }

/*
j*i=(i*j)

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6  3*3=9
1*4=4 2*4=8  3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
    }

测试打印5行三角形

public class TestDemo01 {
    public static void main(String[] args) {

        /*
        * 双重for循环,外层为行,内层为列
        */
/*
第一步
     *****
     ****
     ***
     **
     *

第二步
     *
     **
     ***
     ****
     *****

第三步
        *
       ***
      *****
     *******
    *********
*/
        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();
        }

    }
}

Debug

检查逻辑

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值