Basic knowledge of Java in practice (first day)

1. Understand the basic types of parameter passing in Java: value passing

public static void main (String[] args){
    
    
	int a = 10;
	change(a);
	System.out.println(a); // a=10
	}
	public static void change(int a ){
    
    
	 	System.out.println(a); //a=10
	 	a = 20;
	 	System.out.println(a);  //a=20
	 	}
	 }
这里是比较常考的Java面试题,主要难点在于两点:
1. 形参和实参各指什么?
形参:以方法为例,就是方法定义时的变量
实参:在方法内部定义的变量
2. Java的参数传递机制是什么样的?
值传递,传输的是实参存储的值

Compare the following code with the above to see the difference

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        // 目标:理解引用类型的参数传递机制:值传递,区分其不同点
        int[] arrs = {
    
    10 ,20 ,30};
        change(arrs);
        System.out.println(arrs[1]);  //222
    }
    public static void change(int[] arrs){
    
    
        System.out.println(arrs[1]); //20
        arrs[1] = 222;
        System.out.println(arrs[1]); ///222
    }
}

2. Print the contents of any integer array

Define a method that can be called to print the contents of any integer array

public class Test7 {
    
    
    public static void main(String[] args) {
    
    
        // 需求:调用printArray可以打印任意整型数组的内容
        int[] ages = new int[]{
    
    10 ,20 ,25 ,18};
        printArray(ages);

    }
    public static void printArray(int [] arr){
    
    
        System.out.print("[");
        //这里做一个判断,作用是除了最后一个元素之外,其他的元素后面加逗号
        for (int i = 0; i < arr.length; i++) {
    
    
            if(i == arr.length - 1){
    
    
                System.out.print(arr[i]);
            }else {
    
    
                System.out.print(arr[i]+",");
            }
        }
        System.out.println("]");
    }
}

There is another way to achieve this, which is to use ternary characters to determine whether it is the last element of the array in one line of code.

3. Query the index of a certain data from the integer array and return -1 if it does not exist.

public class Test8 {
    
    
    public static void main(String[] args) {
    
    
        //需求:从整型数组中查询某个数据的索引并返回,不存在该数据就返回-1
        //定义数组,调用方法
        int[] arr = {
    
    11 , 22 , 33 , 66 , 87 , 16};
        int index = searchIndex(arr,87);
        System.out.println("你查询的数据的索引是:" + index);

    }
    //需要返回值的话,就需要定义实参
    public static int searchIndex(int[] arr, int data){
    
    
        //找出数据的索引,采用的是遍历查询
        for (int i = 0; i < arr.length; i++) {
    
    
            if(arr[i] == data){
    
    
                return i;
            }
        }
        return -1;  //在数组中没有这个元素
    }
}

4. Compare the contents of any two integer arrays to see if they are the same. If they are the same, return true; if they are different, return false.

public class Test9 {
    
    
    public static void main(String[] args) {
    
    
        //比较任意2个整型数组的内容是否一样,一样返回true,反之返回false
        //定义两个数组,然后调用compare方法
        int[] arr1 = new int[]{
    
    10 , 11 , 12 ,13};
        int[] arr2 = new int[]{
    
    10 , 11 , 12 , 13};
        boolean result= compare(arr1 , arr2);
        System.out.println("两组数据的内容是否一样: " + result);

    }
     // 需求返回true或者false 所以这里用布尔类型
    public static boolean compare(int[] arr1 , int[] arr2){
    
    
        // 判断两个数组的内容是一样的
        if (arr1.length == arr2.length){
    
    
            for (int i = 0; i < arr1.length; i++) {
    
    
                if(arr1[i] != arr2[i]){
    
    
                    return false;
                }
            }
            return true;
        }else {
    
    
            return false;
        }
    }
}

5. Method overloading

什么是方法重载:
在同一个类中,出现多个方法名称相同,但是形参列表是不同的,那么这些方法就是重载方法
举个代码例子
public class Test10 {
    
    
    public static void main(String[] args) {
    
    
        //认识什么是方法重载?
        fire();
        fire("日本");
        fire("日本", 100000);
    }
    public static void fire(){
    
    
        fire("日本");
    }
    public static void fire(String location){
    
    
        fire(location,100000);
    }
    public static void fire(String location , int number){
    
    
        System.out.println("默认发射"+number+"枚武器给"+location+"```");
    }
}
E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=60873:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test10
默认发射100000枚武器给日本```
默认发射100000枚武器给日本```
默认发射100000枚武器给日本```

两个重点问题:
1.方法重载是什么样的?
同一个类中,多个方法的名称相同,形参列表不同。
2.使用方法重载的好处?
对于相似功能的业务场景:可读性好,方法名称相同提示是同一类型的的功能,通过形参不同实现功能差异化的选择,这是一种专业的代码设计。
方法重载的识别技巧:
1.只要是在同一个类中,方法名称相同,形参列表不同,那么他们就是重载的方法,其他的都不管
2.形参列表的不同指的是:形参的个数,类型,顺序不同,不关心形参的名称
方法重载的关键要求是什么样的?
1.在同一个类中,多个方法的名称不同,形参列表不同,其他的无所谓
形参列表不同指的是什么?
形参的个数,类型,顺序不同。不关心形参的名称。

6. The function of return keyword

	可以立即跳出并结束当前方法的执行;return关键字单独使用可以放在任何方法中
废话不多说,直接上代码。
public class Test11 {
    
    
    public static void main(String[] args) {
    
    
        //明确return关键字的作用
        chu(10,0);
    }

    public static void chu(int a , int b){
    
    
        if(b == 0){
    
    
            System.out.println("你输入的数据有问题,除数不能是0");
            return; //立即跳出当前方法,并结束当前方法的执行
        }

        int c = a / b;
        System.out.println("结果是:" + c);
    }
}
E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=62816:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test11
你输入的数据有问题,除数不能是0

Process finished with exit code 0

7. Case Exercise (1) Buying Air Tickets

需求:
用户输入机票原价,在旺季经济舱打85折,头等舱打9折。在淡季经济舱打65折,头等舱打7折。定义一个方法输出经过打折后的机票价格。
代码参考如下:
public class Test5 {
    
    
    public static void main(String[] args) {
    
    
        //录入价格,仓位以及月份 需要用到scanner扫描器
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票原价:");
        double price = sc.nextDouble();
        System.out.println("请输入月份:");
        int month = sc.nextInt();
        System.out.println("请输入仓位类型(头等舱或经济舱):");
        String type = sc.next();
        //然后调用下面定义的方法 calc
         double result = calc(price , month , type);
        System.out.println("你当前购买机票的价格是:"+  result);

    }
    // 需要返回机票的价格因此是double类型
    public static double calc(double money , int month , String type){
    
    
        //判断月份是淡季还是旺季
        if (month >= 5 && month <= 10){
    
    
            //是旺季,然后进行判断是头等舱还是经济舱,因为只有这两个类型,因此我们选择switch语句
            switch(type){
    
    
                case "经济舱":
                    money *= 0.85;
                    break;
                case "头等舱":
                    money *= 0.9;
                    break;
                default:
                    System.out.println("你输入的仓位不正确");
                    money = -1;
            }
        }else if(month == 11 || month == 12 || month >= 1 && month <=4){
    
    
            //代表是淡季 这里还是只有经济舱和头等舱两个选项,因此还是采用switch语句
            switch(type){
    
    
                case "经济舱":
                    money *= 0.65;
                    break;
                case "头等舱":
                    money *= 0.7;
                    break;
                    default:
                        System.out.println("你输入的仓位不正确");
                        money = -1;
            }

        }else{
    
    
            System.out.println("你输入的月份有问题");
            money = -1;
        }
        return money;
    }
}

E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=62954:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test5
请输入机票原价:
1000
请输入月份:
7
请输入仓位类型(头等舱或经济舱):
头等舱
你当前购买机票的价格是:900.0

Process finished with exit code 0

8. Case Exercise (2) Finding Prime Numbers

需求:101-200之间的数据可以采用循环依次拿到;每拿到一个数,判断该数是不是素数。
补充说明:素数是除了1和它本身以外,不能被其他正整数整除,就叫素数
详细的实现思想,从2遍历到该数的一般的数据,看是否有数据可以整除它,没有就代表是素数。
public class Test6 {
    
    
    public static void main(String[] args) {
    
    
        // 需求,找出101到200之间的数据中的素数,并输出
        // 首先先定义一个循环,找到101-200之间的全部数据
        for (int i = 101; i <= 200; i++) {
    
    
            //然后定义一个flag 如果下面的结果不变就代表是素数,不变就不是素数
            boolean flag = true;
            // 判断遍历这个数据是否是素数
            for (int j = 2; j < i / 2; j++) {
    
    
                if(i % j ==0){
    
    
                    flag = false;
                }
            }
            if(flag){
    
    
                System.out.print(i+"\t");
            }
        }
    }
}

E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=63005:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test6
101	103	107	109	113	127	131	137	139	149	151	157	163	167	173	179	181	191	193	197	199	
Process finished with exit code 0

9. Case exercise (3) Develop verification code

目标:
定义一个方法,可以随机产生验证码
随机验证码的核心逻辑:
1.定义一个String类型的变量存储验证码字符
2.定义一个for循环,需要几位验证码就循环几次
3.随机生成0|1|2的数据,依次代表当前位置要生成数字|大写字母|小写字母
4.把0.1.2.交给switch生成对应类型的随机字符,把字符交给String变量
5.循环结束后,返回String类型的变量既是所求
import java.util.Random;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        //定义一个方法返回一个随机验证码 //返回值是string类型的
        String code = creatCode(5);
        System.out.println("随机生成的验证码是:"+ code);

    }
    public static String creatCode(int n){
    
    
        //定义一个for循环,循环n次 ,依次生成随机字符
        String code = "";
        Random r = new Random();
        for (int i = 0; i < n ; i++) {
    
    
            //随机生成 0,1,2来决定生成:大写字母,小写字母,数字
            int type = r.nextInt(3);
            switch (type){
    
    
                case 0 :
                    //假如生成的是0 就代表产生大写字母
                    //再随机产生26个大写字母 然后给code赋值
                    // 大写字母是char类型,需要强制转化 A等于65
                    char ch = (char)(r.nextInt(26)+65);
                    code += ch;
                    break;
                case 1:
                    //假如生成的是1,就代表产生小写字母 同理 a等于95
                    char ch1 = (char)(r.nextInt(26)+97);
                    code += ch1;
                    break;
                case 2:
                    //假如生成的是2,就代表产生的是0-9之间的数字
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}

E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=63040:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test2
随机生成的验证码是:6KSeA

Process finished with exit code 0

10. Case exercise (4) Copying array elements

目标:
将一组数组的元素复制到另一个数组中,可能会有人说直接 数组1=数组2不就行了
但是其实复制的仅仅是数组1的地址而已。
常见的面试题:
数组的拷贝是什么意思:
答:需要创建新数组,把原来数组的元素赋值过来
public class Test4 {
    
    
    public static void main(String[] args) {
    
    
        // 将一组数组中的元素,复制到另一数组中,一般的赋值是只能赋值他存储的地址
        // 定义一个数组
        int[] arr1 = new int[]{
    
    12, 13 ,14, 15};
        //定义 arr2的数组元素的个数
        int[] arr2 = new int[arr1.length];
        //调用 printArray和copy两个方法
        copy(arr1 , arr2 );

        printArray(arr1);
        printArray(arr2);

    }
    // 我们将这个写成一个方法一遍调用 不需要返回值因此可以写成void
    public static void printArray(int [] arr){
    
    
        System.out.print("[");
        for (int i = 0; i < arr.length ; i++) {
    
    
            //下面这个语句主要是规范 输出的数组之间的逗号问题 采用的是三元字符
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
        }
        System.out.println("]");

    }

    public static void copy(int[] arr1, int[] arr2){
    
    
        // 这里遍历 arr1数组中的每一个元素,然后再将每一个元素赋值到 arr2中
        for (int i = 0; i < arr1.length; i++) {
    
    
            arr2[i] = arr1[i];
        }
    }
}

E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=63071:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test4
[12, 13, 14, 15]
[12, 13, 14, 15]

Process finished with exit code 0

11. Case Exercise (5) Judges’ Scoring

目标:
将n个评委的评分输入,找出最高分,最低分,并去除最高分和最低分之后计算出平均分
如何实现评委打分案列?
1.定义一个动态初始化的数组用于存储分数数据
2.定义三个变量用于保存最大值,最小值和总和
3.遍历数组中的每个元素,依次进行统计
4.遍历结束后按照规则计算出结果即可
import java.util.Scanner;

public class Test3 {
    
    
    public static void main(String[] args) {
    
    
        //评委打分,需要输入评委打的分数,然后找出最大值,最小值,以及除去最大值和最小值之后的平均分

        //首先运用scanner扫描器,输入分数,因此采用动态化数组
        int[] scores = new int[6];

        //  然后输入6位评委的分数,因为6位评委比较多,所以采用for循环比较方便
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length ; i++) {
    
    
            System.out.println("请你输入第"+(i + 1) + "个评委的分数:");
            int score = sc.nextInt();
            // 输入完分数之后,将每个个分数存入对应的数组位置
            scores[i] = score;
        }
        // 遍历数组中的每一个数组,找出最大值,最小值,总分
        // 比较值一般拿数组的第一个值来做对比,因此定义第一个值为最大(小)值
        int max = scores[0];
        int min = scores[0];
        int sum = 0;
        for (int i = 0; i < scores.length ; i++) {
    
    
            if (scores[i] > max){
    
    
                // 这个遍历值比它大就替换最大值
                max = scores[i];
            }

            if (scores[i] < min){
    
    
                //遍历值比它小就替换最小值
                min = scores[i];
            }
            sum += scores[i];
        }
        System.out.println("最大值为:" + max );
        System.out.println("最小值为:" + min );
        double result = (double)(sum - max - min ) / (scores.length - 2);
        System.out.println("选手的最终得分是:" + result);
    }

E:\java\jdk-11.0.9\bin\java.exe "-javaagent:E:\soft\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=63119:E:\soft\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath E:\java\example\out\production\Array3 Array.Test3
请你输入第1个评委的分数:
100
请你输入第2个评委的分数:
99
请你输入第3个评委的分数:
93
请你输入第4个评委的分数:
40
请你输入第5个评委的分数:
30
请你输入第6个评委的分数:
12
最大值为:100
最小值为:12
选手的最终得分是:65.5

Process finished with exit code 0

}


Conclusion

It turns out that the world of Java is so big, and it is so difficult to even get a glimpse of a small corner of it. I insist on writing out what I have learned every day. That’s it for the first day, see you tomorrow.

Guess you like

Origin blog.csdn.net/tyloonulinuli/article/details/121518516