java、day8.用户交互(三种循环结构反编译跳出)

1.用户交互(三种循环结构反编译跳出)

1.1cannner介绍

ctrl+右键就可以查看源码 比如Scannner 按住查看类

 package liuchengkongzhi;
 
 import java.util.Scanner;
 
 //流程控制之用户交互输入
 public class Demo2 {
     public static void main(String[] args) {
         //键盘扫描输入
         Scanner scanner = new Scanner(System.in);
         System.out.println("使用next方式接受:");
 
         //判断是否还有输入
         if(scanner.hasNextLine()){
             //如果用的是scanner.hasNext(),打入hello world 那么只会输出hello
             //
             String str = scanner.nextLine();
             System.out.println("输出内容为:"+str);
 
        }
 
         scanner.close();//将键盘扫描关闭,省下资源
    }
 }

1.2next和 nextline的区别

next()

  1. 一定要读取到有效字符才可以结束输入

  2. 对输入有效字符之前遇到的空白字符, next()方法会自动将其去掉

  3. 只有输入有效字符后才将后面的空白作为分隔符或者结束符

  4. next()不能得到带有空白的字符串

nextLine():

1、以enter为结束符,也就是说nextLine()方法返回的是输入回车之前所有字符

2、可以获得空白

1.3scanner进阶

1.3.1一个简单的计算机 几个数字算平均数

 package liuchengkongzhi;
 
 import java.util.Scanner;
 
 public class Demo4 {
     //我们可以输入多个数字,求其总和和平均数, 每输入一个数字回车确认,通过非数字来结束输入并且执行
     public static void main(String[] args) {
 
         Scanner scanner = new Scanner(System.in);
         //和
         double sum=0;
         int m=0;
 
         while (scanner.hasNextInt()){
             int x =scanner.nextInt();
             m++;
             sum=sum+x;
        }
         System.out.println(m+"几个数字"+sum);//每按下一个就有一个回车
         System.out.println(m+"平均值是"+sum/m);
 
         scanner.close();
 
    }
 }
 

2.循环前熟悉if与switch语句

2.1使用循环前准备if语句初使用

 if(){}
 else if(){}
 else if(){}
 else{}

 

2.1.1自行做的一个小游戏 猜年龄

 package struct;
 
 import java.util.Scanner;
 
 public class IfXunHuanDemo {
     //小游戏,猜一猜我的成绩
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         int my_Grade =86;
 
         System.out.println("请输入你的猜想:");
 
         while(scanner.hasNextInt()){
             int a=scanner.nextInt();
             if(a<my_Grade){
                 System.out.println("太少了");
                 System.out.println("请重新输入你的猜想:");
            }else if (a>my_Grade){
                 System.out.println("太多了");
                 System.out.println("请重新输入你的猜想:");
            }else {
                 System.out.println("congratulation: you are right");
                 break;
            }
        }
 
         scanner.close();
 
    }
 }

2.2使用循环前准备switch 多选择结构

使用if else也可以 但是大多数情况下 switch比较方便

 switch(){
     case '':
         语句1;
         break;
     case '':
         语句2;
         break
     case '':
         语句3;
         break
     case '':
         语句4;
         break;
     default:
         语句5;
 }

 

2.2.1switch语句例子,一个输入abc选择句子的语句

注意case穿透加break

 package struct;
 
 import java.util.Scanner;
 
 public class SwitchDemo {
     static String zimu;//字符串
     public static void main(String[] args) {
 
         Scanner scanner = new Scanner(System.in);
         while (scanner.hasNext()) {
             zimu = scanner.next();//我不会输入字符,不知道怎么设置
 
             switch (zimu) {
                 case "a"://现在已经支持字符串的输入“”就是字符串 ‘’就是字符
                     System.out.println("大傻逼");
                     break;//如果没有brek就会有穿透现象当输入为a时,就会有大傻逼,大聪明,呜呜呜
                 case "b":
                     System.out.println("大聪明");
                     break;
                 case "c":
                     System.out.println("呜呜呜");
                     break;
                 default:
                     System.out.println("啥也不是");
            }
        }
    }
 
 }
 

3三种循环结构while do……while for和break continue

3.1while循环

 while(){
     语句;
 }

3.2do……while

和while的区别先执行语句,再进行条件的判断

 do{
             语句
        }while (i<=100);

3.3 for循环

使循环结构更简单,是最有效,最灵活的循环结构

 for(初始化;布尔值;更新){
     语句4
 }

3.3.1for循环注意事项

  1. 初始化可以为空语句

  2. 布尔值也可以为空

  3. 更新液可以为空语句

    for(;;)就是死循环

3.3.2for循环快捷生成

100.for

3.3.3for循环的三个小题目

1:计算0到100之间基数和偶数的和

 package struct;
 
 public class ForDemo {
 
     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);
    }
 }
 

 

2:用while和for循环输出1-1000之间能被5整除的数,并且每行输出3个

 package struct;
 
 public class ForDemo2 {
     public static void main(String[] args) {
         int a =0;
        // char huanhang='\n';//转义序列
         int m=0;
         int i=0;
         int m1=0;
         int i1=0;
 
         for (i = 0; i <= 1000; i++) {
             if (i%5==0){//如果能被5整除那么输出i加一个空字符,并且m加1
                 System.out.print(i+" ");
                 ++m;
                 if (m%3==0)//每当i能被三整除,那就换行
                     //System.out.println("");这样也可以
                // {System.out.print('\n');}
                     //System.out.print(huanhang);也可以
                 if(i%(3*5)==0){}{System.out.print('\n');}//每到15的倍数就进行一次换行
            }
        }
 
         while (i1<=1000){
             if (i1%5==0){//如果能被5整除那么输出i加一个空字符,并且m加1
                 System.out.print(i1+" ");
                 ++m1;
                 if (m1%3==0)//每当i能被三整除,那就换行
                     //System.out.println("");这样也可以
                     System.out.print('\n');//也可以
                 //System.out.print(huanhang);也可以
            }
             i1++;//这个一定不能忘了加
        }
    }
 }

 

3:打印99乘法表

 package struct;
 //自己想的,不是很完美
 public class ForDemo3 {
 
     public static void main(String[] args) {
 
         for (int i = 1; i <= 9; i++) {
             for (int j=1; j <= i; j++) {
                 System.out.print(i+"*"+j+"="+(i*j)+" ");
                  }
             System.out.print('\n');
            }
        }
    }
 
 结果:package struct;
 1*1=1
 2*1=2 2*2=4
 3*1=3 3*2=6 3*3=9
 4*1=4 4*2=8 4*3=12 4*4=16
 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
 
 

 

 

 

4:使用三种方法分别进行1加到100的操作

 package struct;
 
 public class WhileDemo{
     public static void main(String[] args) {
         //计算1加到100
         int sum1=0;
         int sum2=0;
         int sum3=0;
 
 
         int i1=0;
         int i2=0;
         int i3=0;
         //while循环
         while(i1<=100){
             sum1+=i1;
             i1++;
        }
         //for循环
         for(;i2<=100;i2++){
             sum2+=i2;
        }
 
         //do……while循环
         do{
             sum3+=i3;
             i3++;
        }while (i3<=100);
 
         System.out.println("总和:"+sum1);
         System.out.println("总和:"+sum2);
         System.out.println("总和:"+sum3);
    }
 }
 

5.利用for循环来进行打印三角形,(注意掌握思想)

 package struct;
 
 public class TriangleDemo {
     public static void main(String[] args) {
         //打印三角形
         int a=1;
         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();
        }
 
    }
 }
 结果:  
      *
     ***
    *****
   *******
  *********

 

3.4增强for循环

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

3.4.1定义数组

 int[] numbers = {10,20,30,40,50};

3.4.2用增强for循环遍历数组

 public class StrongerFor {
     public static void main(String[] args) {
         int[] numbers = {10,20,30,40,50};
         //遍历数组的元素
         for(int x:numbers){
             System.out.println(x);
        }
    }
 }

3.4 break ,continue

  • break只是跳出循环

  • continue终止某次循环,继续下一次循环

3.5 goto

  • goto关键字 goto很早就出现了,java中并未正式使用

    可以在break和continue中添加标签来实现

3.5.1使用相当于goto的添加标签的continue来实现打印101到150之间的所有质数

 public class GoToDemo {
     public static void main(String[] args) {
         int count = 0;
         outer:for (int i = 101; i < 150; i++) {
             for (int j=2;j<i/2;j++){
                 if (i%j==0){
                     continue  outer;//out就是标签,相当于goto
                     //为什么可以判断不是质数
                     //因为超过了该数本身的一半必不可能是自身的
                }
            }
             System.out.println(i+" ");
 
        }
    }
 }

 

4.反编译

4.1反编译方法

  1. 首先目的是找到class文件,并且将其拖入idea,所以要找到stucture。看下文件输出路径

  2. file-》 project stucture-》找到输出路径就可以了,打开文件

  3. 将其复制到java同文件夹下面就可以打开了

4.2将2.2.1的文件反编译有的收获

  1. 其中的a显示为97,因为本质还是数字

  2. var2.hashCode() switch里面的内容变成了这个 因为每一个对象都会生成自己的harshcode然后比较hashcode值和case的值

 //
 // Source code recreated from a .class file by IntelliJ IDEA
 // (powered by FernFlower decompiler)
 //
 
 package struct;
 
 import java.util.Scanner;
 
 public class SwitchDemo {
     static String zimu;
 
     public SwitchDemo() {
    }
 
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
 
         while(scanner.hasNext()) {
             zimu = scanner.next();
             String var2 = zimu;
             byte var3 = -1;
             switch(var2.hashCode()) {
             case 97://显示为97
                 if (var2.equals("a")) {
                     var3 = 0;
                }
                 break;
             case 98:
                 if (var2.equals("b")) {
                     var3 = 1;
                }
                 break;
             case 99:
                 if (var2.equals("c")) {
                     var3 = 2;
                }
            }
 
             switch(var3) {
             case 0:
                 System.out.println("大傻逼");
                 break;
             case 1:
                 System.out.println("大聪明");
                 break;
             case 2:
                 System.out.println("呜呜呜");
                 break;
             default:
                 System.out.println("啥也不是");
            }
        }
 
    }
 }

 

 

 

 

 

 

 

posted @ 2022-03-18 21:32  newlive122  阅读(172)  评论(0)    收藏  举报