一些JAVA小练习,课后作业啥的可以参考参考

序言

  • 这是笔者无聊时候写的一些java小练习,不排除会持续更新,谁知道呢哈哈哈

小练习

1. 九九乘法表

public class MultiplicationTable {

    public static void main(String[] args) {
        // for循环嵌套
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            System.out.println();
        }
    }
}

2. 求数组中的最大值最小值

public class ArrayMaximum {

    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = {2, 6, 1, 77, 52, 25, 7};
        // 定义最大值最小值,初始值取数组的第一个元素
        int max = arr[0];
        int min = arr[0];
        // 遍历数组,赋值最大最小值
        // 该for循环用的是增强for循环
        for (int i : arr) {
            if (i > max) {
                max = i;
            }
            if (i < min) {
                min = i;
            }
        }
        System.out.println("数组的的最大值:" + max);
        System.out.println("数组的的最小值:" + min);
    }
}

3. 给数组排序,数组默认长度为10,一次录入十个元素,然后排序

public class ArraySort {

    // 定义数组长度默认为10
    private static final int ARRAY_LENGTH = 10;

    // 定义一个数组长度为默认长度的数组,即长度为10的数组
    private static final int[] arr = new int[ARRAY_LENGTH];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 录入10个元素到数组中去
        for (int i = 0; i < arr.length; i++) {
            try {
                System.out.println("请输入第" + (i + 1) + "位元素:");
                String next = sc.next();
                int item = Integer.parseInt(next);
                arr[i] = item;
            } catch (NumberFormatException e) {
                System.out.println("录入数据转化有误,非数字类型!");
            }
        }

        System.out.println("录入数组数据完成");

        // 给数组排序(用的冒泡算法)
        int temp;
        for (int j = 0; j < arr.length - 1; j++) {
            for (int k = 0; k < arr.length - 1; k++) {
                if (arr[k] > arr[k + 1]) {
                    temp = arr[k];
                    arr[k] = arr[k + 1];
                    arr[k + 1] = temp;
                }
            }
        }

        // 打印数组
        System.out.println("排序后的数组:" + Arrays.toString(arr));
    }
}

4. 猜数字

public class GuessNumber {

    public static void main(String[] args) {
        // 猜数字方法
        guessNumber();
    }

    public static void guessNumber() {
        //用户输入数字
        Scanner scanner = new Scanner(System.in);
        // 随机数对象
        Random random = new Random();
        //生成一个11 - 99的随机数
        int randomNum = random.nextInt(99) + 11;
        //计算猜数字的次数
        int count = 0;
        // 这里写的死循环,不建议这么写
        while (true) {
            System.out.println("请输入你猜的数字(11~99):");
            int guessNumber = scanner.nextInt();
            if (guessNumber <= 99 && guessNumber >= 11) {
                //判断数字
                if (guessNumber < randomNum) {
                    System.out.println("你猜的数字小了!");
                    count++;
                } else if (guessNumber > randomNum) {
                    System.out.println("你猜的数字大了!");
                    count++;
                } else {
                    System.out.println("恭喜你猜对了!");
                    System.out.println("你一共猜了:" + count + "次");
                    //判断成功,终止程序
                    break;
                }
            } else {
                System.out.println("数字不在范围内!");
            }
        }
    }
}

5. 输入一个整数,判断奇偶

public class JudgeParity {

    public static void main(String[] args) {
        // 获取输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        String next = sc.next();

        // 判断奇偶
        try {
            int number = Integer.parseInt(next);
            // 整除2余数为0即为整数
            if (number % 2 == 0) {
                System.out.println("整数" + number + "是偶数");
            } else {
                System.out.println("整数" + number + "是奇数");
            }
        } catch (NumberFormatException e) {
            System.out.println("读取输入不是整数!!!");
        } catch (Exception e) {
            System.out.println("系统异常!");
            e.printStackTrace();
        } finally {
            // 关闭流,因为Scanner是持续使用的,不关闭会造成资源的浪费
            sc.close();
        }
    }
}

6. 输入年份、月份,判断当月天数

public class ObtainMonthlyDays {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        String year = sc.next();
        System.out.println("请输入月份");
        String month = sc.next();

        try {
            int yearNum = Integer.parseInt(year);
            int monthNum = Integer.parseInt(month);

            switch (monthNum) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    System.out.println(yearNum + "年" + monthNum + "月为31天");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    System.out.println(yearNum + "年" + monthNum + "月为30天");
                    break;
                case 2:
                    if (yearNum % 4 == 0 && yearNum % 100 != 0 || yearNum % 400 == 0) {
                        System.out.println(yearNum + "年" + monthNum + "月为29天");
                    } else {
                        System.out.println(yearNum + "年" + monthNum + "月为28天");
                    }
                    break;
                default:
                    System.out.println("月份数字不在1-12内,请检查月份");
                    break;
            }
        } catch (NumberFormatException e) {
            System.out.println("输入年份或月份有误!!!");
        } catch (Exception e) {
            System.out.println("系统异常!");
            e.printStackTrace();
        } finally {
            // 关闭流,
            sc.close();
        }
    }
}

7. 输出1-100之间所有的整数,每五个换行

public class OutNumber {

    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            count++;
            System.out.print(i + "\t");
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }
}

8. 输入密码验证,密码错误提示错误,正确则提示进入系统

public class PasswordVerification {

    // 定义系统密码
    private static final String SYSTEM_PASSWORD = "123456";

    public static void main(String[] args)  {
        // 获取用户输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入系统密码:");

        while (true){
            String password = sc.next();
            // 为了更有真实性,我们在校验密码的时候让线程睡眠一秒钟,就是等待一秒钟后在给验证结果
            // 不过我们不建议在死循环里面实用线程睡眠,不过这里只有一个main线程,没有关系
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 判断密码
            if (SYSTEM_PASSWORD.equals(password)){
                System.out.println("密码正确,欢迎进入系统");
                // 关闭流
                sc.close();
                // 退出程序
                System.exit(0);
            } else {
                System.out.println("密码错误哈!再试再试!");
                System.out.println("请再次输入密码:");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值