Java控制流,反编译,打印三角形

用户交互Scanner

Scanner中的判断目前自己学的常用的方法有hasNext() //判断用户有没有输入字符。hasNextLine()。 //判断用户是否输入完成

  • hasNext()代码如下:

    import java.util.Scanner;
    
    public class Demo02 {
    
        public static void main(String[] args) {
            //创建一个扫描器对象,用于接受键盘数据
            Scanner scanner = new Scanner(System.in);
    
    
            //判断用户是否还有输入
            if (scanner.hasNextLine()) {
    
                String str = scanner.nextLine();
                System.out.println("输出的结果为:"+str);
            }
        }
    }
    
  • hasNextLine()代码如下:

    import java.util.Scanner;
    
    public class Demo02 {
    
        public static void main(String[] args) {
            //创建一个扫描器对象,用于接受键盘数据
            Scanner scanner = new Scanner(System.in);
    
    
            //判断用户是否还有输入
            if (scanner.hasNextLine()) {
    
                String str = scanner.nextLine();
                System.out.println("输出的结果为:"+str);
            }
                 scanner.close();
        }
    }
    
注意事项

- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oMpsjpzK-1630633726776)(C:\Users\lenovo\Desktop\QQ图片20210901102034.png)])

小练习

  • 我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果

    代码如下:

    package Scanner;
    
    import java.util.Scanner;
    
    public class Demo05 {
        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++;
                sum = sum + x;
                System.out.println("你输入了第"+m+"个数字,然后当前结果为:"+sum);
            }
            System.out.println(m+"个数字的和为:"+sum);
            System.out.println(m+"个数字的平均数为:"+sum/m);
            scanner.close();
        }
    }
    
    

顺序结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0vu4dzMG-1630633873526)(C:\Users\lenovo\Desktop\QQ图片20210901164522.png)]

选择结构

If单选择

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dzziXyth-1630633915788)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901182952163.png)]

单项选择代码:

package struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        //New  一个scanner对象,以便于之后操作方法
        Scanner scanner = new Scanner(System.in);

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

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

If双选择

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-guuDxYK6-1630633726780)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901183952486.png)]

双选择代码

package struct;

import java.util.Scanner;

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

    }

}

If多选择

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xIEdI52m-1630633726781)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901184508537.png)]

多选代码

package struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60为不及格

        Scanner scanner = new Scanner(System.in);

        /*
        if 语句至多有 1 个else语句,else 语句在所有的else if语句之后,
        if 语句可以有若干个else if语句,他们必须在else语句之前。
        一旦其中一个else if语句检测为 true,其他的else if 语句直接跳过执行
         */
        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)) {
            System.out.println("不合格");
        }
        else{
            System.out.println("成绩不合法");
        }
        scanner.close();
    }
}

嵌套if

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qmBmjwOm-1630633726782)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901185952413.png)]

Switch结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fl7cagUV-1630633726782)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901204523080.png)]

Switch代码

package struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case 穿透,如果没有break,则会一直执行到break或者default
        char grade = 'C';

        switch (grade){
            case 'A':
                System.out.println("优秀");
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("再接再厉");
            case 'E':
                System.out.println("挂科~");
        }

    }
}

  • 注意:还需要补充default,若switch(变量或者表达式)中,下面对应没有值,则会自动跳到default。

循环结构

while循环

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QPc6gGGl-1630633726783)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901214350603.png)]

while循环代码
package struct;

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

        //输出1~100
        int i  =  0;

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

do while

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7NOVzA4L-1630633726784)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901220227287.png)]

For循环

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VMbQJ9Ir-1630633726784)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901220817680.png)]

代码:

package 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循环结束!");
        
           //死循环
        for (int i = 1;;i++){
            System.out.println("死循环"+i);
        }
        
    }
}

练习1: 计算0-100之间的技术和偶数的和
package struct;

public class ForDemo02 {
    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) { //奇数
                oddSum += i;
            }else {
                evenSum+= i;
            }
        }
        System.out.println("偶数和为:"+evenSum);
        System.out.println("奇数和为:"+oddSum);

    }
}
练习2:用while或For循环输出1-1000之间能被5整除的数,并且每行输出三个
package struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或For循环输出1-1000之间能被5整除的数,并且每行输出三个

//        for (int i = 0; i < 1000; i++) {
//            if (i % 5 == 0) {
//                System.out.print(i + "\t");
//            }
//            if (i % 15 == 0) {
//                System.out.println();
//            }
//            //print不会换行
//            //println输出完换行
//        }
        //用while来实现
        int i = 0;
        while (i < 1000) {
            if (i % 5 == 0) {
                System.out.print( i + "\t");
            }
            if (i % 15 == 0) {
                System.out.println();
            }
            i++;
        }
    }
}
练习3:打印九九乘法表
package struct;

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

public class ForDemo05 {
    public static void main(String[] args) {
        int[] nums ={10,25,30,50,60,80,80};

        //用for循环来实现遍历数组
        for (int i = 0; i < nums.length; i++) {
            System.out.println(nums[i]);
        }
        System.out.println("===========================");
        //遍历数组元素,用加强for循环来实现遍历数组
        for (int x : nums){
            System.out.println(x);
        }
    }
}

break & continue

  • break是指在循环中直接跳出循环,

  • continue在循环中,直接跳到循环处

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XWBUL7xD-1630633726785)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210903091442210.png)]

关于goto关键字:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1aimReja-1630633726786)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210903091515280.png)]

代码:
package struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        //质数是指大于1的自然数中,除了1和它本身之外不再有任何因数的自然数

        //不建议使用标签
        outer:for (int i = 101; i < 150; i++) {
            for (int j=2 ; j<i/2;j++){
                if (i%j==0) {
                   continue outer;
                }
            }
            System.out.print(i+"\t");
        }
    }
}

编译反编译

反编译用到的软件有IDEA,也是本编译软件。

  • 首先打开项目结构,找到Project compiler output(项目编译输出的地方):【我的在D盘:\Practice2\out下面】

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-30LuMLFM-1630633726787)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901211533592.png)]

  • 其次找到想要反编译的.class文件:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PeYQm8aX-1630633726787)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901211749629.png)]

  • 然后找到.java所在的项目,将.class文件拖入到.java所在文件,Idea会自动反编译:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L9pzIPCG-1630633726789)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210901211908602.png)]

打印三角形:

package struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印三角形 5行

        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {          //j大于等于 i
                System.out.print(" ");
            }
            for (int j = 1; j <=i ; j++) {          //J小于等于 i
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {           //J小于    i
                System.out.print("*");
            }
            System.out.println();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱折磨键盘的大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值