栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java基础day06

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java基础day06

掌握debug的使用
public class Demo01 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        //int x = 1/0; //ArithmeticException
        int c = a+b;

        System.out.println(c);

        getSum();
        System.out.println("main方法结束");
    }

    private static void getSum() {
        int x = 10;
        int y = 20;
        int z = x+y;
        System.out.println(z);

    }
}
进制 进制划分
public class Demo01 {
    public static void main(String[] args) {
        System.out.println(111);
        System.out.println(0b111);
        System.out.println(0111);
        System.out.println(0x111);
    }
}
任意进制到十进制的转换
public class Demo02 {
    public static void main(String[] args) {

    }
}
十进制到任意进制的转换
public class Demo03 {
}
快速进制转换法
public class Demo04 {
}
原码反码补码

注意:计算机中的数据,都是以二进制补码的形式在运算,而补码则是通过反码和原码推算出来的。

原码(可直观看出数据大小)
就是二进制定点表示法,即最高位为符号位,【0】表示正,【1】表示负,其余位表示数值的大小。
通过一个字节表示+7和-7,代码:byte b1 = 7;   byte b2 = -7;
一个字节等于8个比特位,也就是8个二进制位
	0(符号位)	0000111
 	1(符号位)	000011

反码
正数的反码与其原码相同;负数的反码是对其原码逐位取反,但符号位除外。

补码(数据以该状态进行运算)
正数的补码与其原码相同;负数的补码是在其反码的末位加1。

正数的原反补都是相同的
负数的【反码】,是根据【原码】取反(0变1,1变0)得到的 (符号位不变)
负数的【补码】,是根据【反码】的末尾+1,得到的
位运算

位运算的练习
public class Demo01 {
    public static void main(String[] args) {
        //&:见0则0
        System.out.println(5&3);
        //|:见1则1
        System.out.println(5|3);
        //~:取反 0变1 1变0 包括符号位
        System.out.println(~5);
        //^:相同为0 不同为1
        System.out.println(5^3);
        //<<:左移几位相等乘以2的几次幂  右边补齐0
        System.out.println(5<<1);
        System.out.println(5<<2);
        System.out.println(5<<3);
        //>>:右移几位相等除以2的几次幂 根据符号位,补齐左边
        System.out.println(40>>1);
        System.out.println(40>>2);
        System.out.println(40>>3);
        //>>>:无符号右移,无论最符号位是0还是1,都补0
        System.out.println(40>>>1);
        System.out.println(40>>>2);
        System.out.println(40>>>3);

        System.out.println(-8>>>1);

    }
}
异或运算的特点
public class Demo02 {
    public static void main(String[] args) {
        System.out.println(9^8^8);//9
        System.out.println(8^9^8);//9
        System.out.println(8^8^9);//9
        System.out.println(7^8^9);//6
    }
}
数据交换
public class Demo03数据交换 {
    public static void main(String[] args) {

        int a = 10;
        int b = 20;
        //changeNum(a, b);
        //不允许使用三方变量
        a = a^b;//10^20
        b = a^b;//10^20^20 == 10
        a = a^b;//10^20^10 == 20

        System.out.println(a);
        System.out.println(b);
    }

    //借用第三方变量 实现数据交换
    private static void changeNum(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
        System.out.println(a);
        System.out.println(b);
    }
}
数组元素反转
public class Demo04数组元素反转 {
    public static void main(String[] args) {
        int[] arr = {19, 28, 37, 46, 50};
        //使用for循环
        for (int start = 0, end = arr.length - 1; start < end; start++, end--) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        //打印数组
        System.out.println(Arrays.toString(arr));
    }
}
二维数组 定位

二维数组也是一种容器,不同于一维数组,该容器存储的都是一维数组容器
格式1:数据类型 [ ] [ ] 变量名;
范例:int [ ] [ ] arr;

格式2:数据类型 变量名[ ] [ ];
格式3:数据类型[ ] 变量名[ ];
范例:int arr[ ] [ ];
范例:int[ ] arr[ ];

动态初始化

格式:数据类型 [ ] [ ] 变量名 = new 数据类型[ m ] [ n ];
m表示这个二维数组,可以存放多少个一维数组
n表示每一个一维数组,可以存放多少个元素

范例:int [ ] [ ] arr = new int [ 2 ][ 3 ];

该数组可以存放2个一维数组,每个一维数组中可以存放3个int类型元素

二维数组静态初始化

格式:数据类型 [ ] [ ] 变量名 = new 数据类型[ ] [ ] { {元素1,元素2}, {元素1, 元素2} };
范例:int [ ] [ ] arr = new int [ ][ ] { {11,22} , {33,44} };

简化格式:数据类型 [ ] [ ] 变量名 = { {元素1,元素2}, {元素1, 元素2} };
范例:int [ ] [ ] arr = { {11,22} , {33,44} };

二维数组遍历
public class Demo03 {
    public static void main(String[] args) {
        int[][] arr = {{11, 22, 33}, {33, 44, 55}};
        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
            int [] brr = arr[i];
            for (int j = 0; j < brr.length; j++) {
                System.out.println(brr[j]);
            }
        }

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }

    }
}
二维数组求和
public class Demo04公司年销售额求和 {
    public static void main(String[] args) {
        int [] season1 = {22,66,44};
        int [] season2 = {77,33,88};
        int [] season3 = {25,45,65};
        int [] season4 = {11,66,99};
        int [][] yearSales = {season1,season2,season3,season4};
        int [][] totalSales = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
        //1.定义求和变量
        int sum = 0;
        //2.遍历二维数组获取每一个一维数组
        for (int i = 0; i < yearSales.length; i++) {
            int [] seasonSales = yearSales[i];
            //3.遍历每一个一维数组,获取每个月的销售额
            for (int j = 0; j < seasonSales.length; j++) {
                //4.和求和变量累加
                sum+=seasonSales[j];
            }
        }
        //5.打印结果
        System.out.println("公司年销售额:"+sum+"万元!");
    }
}
求最大值
public class Demo05公司月最高销售额 {
    public static void main(String[] args) {
        int [][] totalSales = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
        //1.定义最高销售额 默认第一季度的第一个月
        int max = totalSales[0][0];
        //2.遍历年销售额数据 totalSales 二维数组
        for (int i = 0; i < totalSales.length; i++) {
            //遍历每个季度的销售额
            for (int j = 0; j < totalSales[i].length; j++) {
                //比较
                if (totalSales[i][j]>max){
                    //更新最大值
                    max = totalSales[i][j];
                }
            }
        }
        //3.打印最大值
        System.out.println("max = "+max);
    }
}
练习 机票
import java.util.Scanner;


public class Demo01买飞机票 {
    public static void main(String[] args) {
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您选择舱位:");
        String cw = scanner.nextLine();
        //使用分支语句判断
        switch (cw) {
            case "头等舱":
                System.out.println("请输入您乘机的月份:");
                int month = scanner.nextInt();
                System.out.println("请输入您机票原价:");
                double price = scanner.nextDouble();
                if (month >= 5 && month <= 10) {//旺季
                    price *= 0.9;
                } else if (month == 11 || month == 12 || month >= 1 && month <= 4) {//淡季
                    price *= 0.7;
                } else {
                    System.out.println("您输入的月份有误!");
                }
                System.out.println("您的机票价格:"+price+"元");
                break;
            case "经济舱":
                System.out.println("请输入您乘机的月份:");
                int month1 = scanner.nextInt();
                System.out.println("请输入您机票原价:");
                double price1 = scanner.nextDouble();
                if (month1 >= 5 && month1 <= 10) {//旺季
                    price1*= 0.85;
                }else if (month1 == 11 || month1 == 12 || month1 >= 1 && month1 <= 4){//淡季
                    price1*= 0.65;
                }else {
                    System.out.println("您输入的月份有误!");
                }
                System.out.println("您的机票价格:"+price1+"元");
                break;
            default:
                System.out.println("您选择舱位不存在!");
        }
    }
}
练习 求质数
public class Demo02求质数 {
    public static void main(String[] args) {
        //外层循环取到101到200之间的所有的数字
        for (int num = 101; num <= 200; num++) {
            //定义第三变量 默认数字是质数 第三方变量值是true
            boolean flag = true;
            for (int i = 2; i < num/2; i++) {
                if (num%i == 0){
                    flag = false;
                    break;
                }
            }
            if (flag){
                System.out.println(num+"是质数");
            }else {
                System.out.println(num+"不是质数");
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号