3.23·Java基础学习

  1. 三目运算符

// 要和主函数同一个层级
public static void maxnum(int a , int b){
        int maxnum1 = a >= b ? a : b;
        System.out.println("最大值为"+ maxnum1);
    }
  1. 函数方法的调用

方法没有被调用的时候,在方法区里的字节码文件里存放

方法被调用的时候,需要进入到栈内存里面去运行哦

import java.util.Scanner;

public class wang{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入名字哦");
        String name = sc.nextLine();
        System.out.println("姓名为:"+ name);
        int a = 130;
        byte b = (byte)a;
        System.out.println(b);
        int num1, num2, num3;
        num1 = sc.nextInt(); num2 = sc.nextInt(); num3 = sc.nextInt();
        int maxnum = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
        System.out.println("输入的三个数中最大的数值为"+ maxnum);
        int max1 = maxnum(10, 20);
        int max2 = maxnum(30, 40);
        System.out.println(max1+ " " + max2);
    }

    public static int maxnum(int a , int b){
        int maxnum1 = a >= b ? a : b;
        System.out.println("最大值为"+ maxnum1);
        return maxnum1;
    }
}
  1. 方法重载

->重载的规则

day3:总结

4.switch语句

->switch语句的注意事项

->优化

->case穿透现象

执行对应的case没有break;那就一直走下去找到对应break才罢休

5.循环和随机变量

import java.util.Random;
import java.util.Scanner;

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

        lo: // 标号:给循环取名字哦
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入选择哦");
            int choice = sc.nextInt();

            switch(choice){
                case 1:
                    System.out.println(1);
                    break;
                case 2:
                    System.out.println("感谢你的使用哦");
                    // 将while循环结束掉咯,仅仅写一个break就退出switch而没有跳出while循环哦
                    break lo;   
            }
            
        }

        Random r = new Random();
        for(int i = 0 ; i < 10 ; i++){
            int num = r.nextInt(61)+20;
            if(num == 69){
                System.out.println("play");
            }
            System.out.println(num);
        }
        
    }
}

补充:Java自动补全功能

输出打印:sout+Enter

错误打印:serr+Enter

main主函数 :psvm+Enter

if判断:变量.if+Enter 比如 a>0.if || true.if if前必须是一个boolean类型的变量或者表达式

switch:变量.switch+Enter

try捕获异常:变量.try+Enter

Collection对象for循环:对象.for+Enter

int变量递增for循环 :int变量.fori+Enter

int变量递减for循环:int变量.forr+Enter

判断变量是否为空:变量.null+Enter

判断变量是否非空:变量.notnull+Enter 或者 变量.nn+Enter

补充内容(快速化操作)

批量修改变量名

右击——更改所以匹配项

从main里面抽取一个方法

选中对应内容——小灯泡——extension to method就好啦

6.数组

静态初始化

int arr[] = { 1, 3, 6, 4, 3 };
int[] arr1 = { 2, 4, 5, 7, 7 };

动态初始化

int[] arr2 = new int[3];
// 后面那个3代表我要开几个int类型的空间,不是代表初始化为3哦

方法中参数传递问题

值传递:基本数据类型

传递进去的其实是开了一个新的变量哦,在方法里面操作的都是对这个新的变量操作。

对原数值没有任何影响呢

地址传递:引用数据类型

传递进去的是一个地址啦,接下来都是方法对地址内容进行操作咯。所以值会发生改变耶

数组内存图

day4:总结

day5:总结

7.补充面试题(不借助第三方变量来换值)

        // 不借助第三方变量来将a和b两个值对换
        int a = 10 ; int b = 20;
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println(a+" "+b);
        

8.对这两天知识总结(来几道题)

import java.util.Random;
import java.util.Scanner;

import javax.xml.validation.Schema;

public class wang {
    public static void main(String[] args) {
        getSeven();

        System.out.println();
        int[] arr = { 68, 27, 95, 88, 171, 996, 51, 210 };
        int sum = getsum(arr);
        System.out.println(sum);

        int[] arr1 = { 1, 3, 5, 7 };
        int[] arr2 = { 1, 3, 5, 7 };
        System.out.println(cheakArray(arr1, arr2));

        int[] arr_1 = { 19, 28, 37, 46, 50 };
        System.out.println("请输入一个要找的数字哦");
        Scanner sc = new Scanner(System.in);
        int target = sc.nextInt();
        System.out.println(search(arr_1, target));

        int[] arr_2 = { 19, 28, 37, 46, 50, 19, 19 };
        System.out.println("请输入一个要找的数字哦");
        int target1 = sc.nextInt();
        int[] result = searchAll(arr_2, target1);
        if (result.length == 0) {
            System.out.println("没有找到对应的元素哦");
        } else {
            for (int j = 0; j < result.length; j++) {
                System.out.println(result[j]);
            }
        }

        int[] change_arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        changeArray(change_arr);
        for (int i = 0; i < change_arr.length; i++) {
            System.out.print(change_arr[i] + " ");
        }
        System.out.println();

        // 不借助第三方变量来将a和b两个值对换
        int a = 10;
        int b = 20;
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println(a + " " + b);

        char[] chs = InitArray();
        String checkCode = "";
        Random r = new Random();
        for(int i = 0 ; i < 5 ; i++){
            int RandomIndex = r.nextInt(chs.length);
            checkCode += chs[RandomIndex];
        }

        System.out.println("得到的验证码是"+ checkCode);

    }

    private static void getSeven() {
        for (int i = 1; i <= 100; i++) {
            if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
                System.out.print(i + " ");
            } else {
                continue;
            }
        }
    }

    public static int getsum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0 && arr[i] % 10 != 7 && arr[i] / 10 % 10 != 7) {
                sum += arr[i];
            }
        }
        return sum;
    }

    public static boolean cheakArray(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // 返回第一个找到的元素的索引哦
    public static int search(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i;
            }
        }
        return -1;
    }

    // 返回所有要找的元素的索引咯
    public static int[] searchAll(int[] arr, int target) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                count++;
            }
        }
        int[] result = new int[count];
        int index = 0;
        for (int j = 0; j < arr.length; j++) {
            if (arr[j] == target) {
                result[index++] = j;
            }
        }
        return result;
    }

    public static void changeArray(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
    }

    public static char[] InitArray() {
        char[] chs = new char[26+26+10];
        int index = 0;
    
        for(char i = 'a'; i <= 'z'; i++){
            chs[index++] = i;   
        }
        for(char j = 'A'; j <= 'Z'; j++){
            chs[index++] = j;
        }
        for(char c = '0'; c <= '9'; c++){
            chs[index++] = c;
        }

        return chs;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值