【Java基础】【案例】

1.数值拆分

需求:一个三位数,将其拆分为个位、十位、百位后,打印在控制台

public class Operator {
    public static void main(String[] args) {
        int data = 589;

        int ge = data % 10;
        System.out.println(ge);

        int shi = data / 10 % 10;
        System.out.println(shi);

        int bai = data / 100;
        System.out.println(bai);
    }
}

2.求三个整数的最大值

需求:定义三个整数,找出最大值并打印在控制台

分析

1.用三元运算符获取前两个整数的最大值, 并用临时变量保存起来

  • num1 > num2 ? num1 : num2;

2.用三元运算符,让临时最大值和第三个整数进行比较,并记录结果

  • temp > num3 ? temp : num3;

3.输出结果

public class Max {
    public static void main(String[] args) {
        int i = 10;
        int j = 30;
        int k = 50;
        int temp = i > j ? i : j;
        int max = temp > k ? temp : k;
        System.out.println(max);
    }
}

3.键盘录入

需求:请完成Java程序与用户交互,比如录入用户输入的名称、年龄。

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

        System.out.println("请输入姓名:");
        String name = scanner.next();
        System.out.println("您的姓名是:" + name);

        System.out.println("请输入年龄:");
        int age = scanner.nextInt();
        System.out.println("您的年龄是:" + age);
    }
}

4.求和

需求:求1-5之间的数据和,并把求和结果在控制台输出

分析:

1.定义for循环使其能够依次访问到1、2、3、4、5.

2.在循环外定义一个整数变量sum用来累加这些数据

3.循环结束后,输出求和变量即是结果

public class Sum {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <=5 ; i++) {
            sum+=i;
        }
        System.out.println("1-5的和是"+sum);
    }
}

5.求奇数和

需求:求1-10之间的奇数和,并把求和结果在控制台输出

分析:

方法一

1.定义for循环使其能够依次访问到:1、2、3......10

2.在for循环中,通过if筛选出奇数

3.在循环外定义一个整数变量sum用来累加这些数据

public class Jishuhe {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 1) {
                sum += i;
            }
        }
        System.out.println("1-10的奇数和是" + sum);
    }
}

方法二

1.定义for循环使其能够依次访问到:1、3、5、7、9

2.在循环外定义一个整数变量sum用来累加这些数据

public class JIshuhe2 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 10; i += 2) {
            sum += i;
        }
        System.out.println("1-10的奇数和是" + sum);
    }
}

6.水仙花数

需求:在控制台输出所有的”水仙花数“,水仙花数必须满足如下2个要求:

1.水仙花数是一个三位数

2.水仙花数的个位、十位、百位数字的立方和等于原数

分析:

1.定义一个for循环从“100一直到999”

2.每次访问到数据后,提取该数据的:个位、十位、百位数字

3.使用if判断:个位、十位、百位数字的立方和是否等于原数,等于则输出该数据

public class Shuixianhua {
    public static void main(String[] args) {
        for (int i = 100; i <= 999; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;
            if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == i) {
                System.out.print(i+"\t");
            }
        }
    }
}

7.珠穆朗玛峰(世界最高峰8848.86)

需求:世界最高山峰是珠穆朗玛峰(8848.86米=8848860毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。请问,折叠多少次,可以折成珠穆朗玛峰的高度

分析:这种不清楚要循环多少次的情况可以选择用while实现

1.定义变量存储珠穆朗玛峰的高度、纸张的高度

2.使用while循环,循环条件是(纸张厚度<山峰高度),内部控制纸张折叠,每折叠一次,纸张厚度为原来的两倍,循环外定义计数变量,每折叠一次让该变量+1

public class Zhumulangma {
    public static void main(String[] args) {
        double peakHeight = 8848860;
        double paperThickness = 0.1;
        int count = 0;
        while (paperThickness < peakHeight) {
            paperThickness *= 2;
            count++;
        }
        System.out.println("折叠的次数" + count);
        System.out.println("纸张的厚度" + paperThickness);
    }
}

8.猜数字游戏

需求:随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜小提示过小,直到猜中             结束游戏

分析:

1.随机生成一个1-100之间的数据

2.使用死循环不断提示用户猜测,猜大提示过大,猜小提示过小,猜中结束游戏

public class Caishuzi {
    public static void main(String[] args) {
        Random r = new Random();
        int luckyNumber = r.nextInt(100)+1;
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请您输入猜测的数据(1-100): ");
            int guessNumber = sc.nextInt();
            if (guessNumber>luckyNumber){
                System.out.println("猜测数据过大");
            }else if(guessNumber<luckyNumber){
                System.out.println("猜测数据过小");
            }else {
                System.out.println("猜中啦");
                break;
            }
        }
    }
}

9.数组遍历-求和

需求:某部门5名员工的销售额分别是:16、26、36、6、100,请计算出他们部门的总销售额

public class Test1 {
    public static void main(String[] args) {
        int[] money = {16, 26, 36, 6, 100};
        int sum = 0;
        for (int i = 0; i < money.length; i++) {
            sum += money[i];
        }
        System.out.println("数组的元素和是" + sum);
    }
}

10.数组求最值

需求:15,9000,10000,20000,6500,-5求最大值

分析:

1.同一类型的数据----数组存储

2.定义一个变量用于记录最大值,这个变量建议默认存储第一个元素值作为参照

3.遍历数组的元素,如果该元素大于变量存储的元素,则替换变量存储的值为该元素

public class Test2 {
    public static void main(String[] args) {
        int[] score = {15,9000,10000,20000,6500,-5};
        int max = score[0];
        for (int i = 1; i < score.length; i++) {
            if (score[i]>max){
                max=score[i];
            }
        }
        System.out.println("最大值为:"+max);
    }
}

11.猜数字游戏

需求:开发一个幸运小游戏,游戏规则如下:

游戏后台随机生成1-20之间的5个数(无所谓是否重复),然后让大家来猜数字:

  • 未猜中提示:“未命中”,并继续猜测
  • 猜中提示:“猜中了”,并输出该数据第一次出现的位置,且输出全部5个数据,最终结束本游戏

分析:

1.随机生成5个1-20之间的数据存储起来-----使用数组

2.定义一个死循环,输入数据猜测,遍历数组,判断数据是否在数组中,如果在,进行对应提示并结束死循环;如果没有猜中,提示继续猜测知道猜中为止。        

public class Caishuzi {
    public static void main(String[] args) {
        int[] data = new int[5];
        Random r = new Random();
        for (int i = 0; i < data.length; i++) {
            data[i] = r.nextInt(20) + 1;
        }
        Scanner sc = new Scanner(System.in);
        OUT:
        while (true) {
            System.out.println("请您输入一个1-20之间的整数进行猜测");
            int guessData = sc.nextInt();
            for (int i = 0; i < data.length; i++) {
                if (data[i] == guessData) {
                    System.out.println("猜中啦!数据索引为:" + i);
                    break OUT;//结束整个死循环
                }
            }
            System.out.println("您猜测的数据在数组中不存在,请重新猜测");
        }
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + "\t");
        }
    }
}

12.随机排名

需求:某公司开发部5名开发人员,要进行项目进展汇报演讲,现在采取随机排名后进行汇报

请先依次录入5名员工的工号,然后展示出一组随机的排名顺序

22 33 35 13 88

分析:

1.在程序中录入5名员工的工号存储起来-----使用数组

2.依次遍历数组中的每个元素,随机一个索引数据,让当前元素与该索引位置处的元素进行交换

public class Test3 {
    public static void main(String[] args) {
        int[] codes = new int[5];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < codes.length; i++) {
            System.out.println("请您输入第" + (i + 1) + "个员工的工号");
            int code = sc.nextInt();
            codes[i] = code;
        }
        Random r = new Random();
        for (int i = 0; i < codes.length; i++) {
            int index = r.nextInt(codes.length);
            int temp = codes[index];
            codes[index] = codes[i];
            codes[i] = temp;
        }
        for (int i = 0; i < codes.length; i++) {
            System.out.print(codes[i] + "\t");
        }
    }
}

13.冒泡排序

  • 冒泡排序的思想:每次从数组中找出最大值放在数组的后面去
  • 实现冒泡排序的关键步骤分析:

        确定总共需要做几轮:数组的长度-1

        每轮比较几次:数组的长度-i

public class Test4 {
    public static void main(String[] args) {
        //定义一个数组,存储一些数据
        int[] arr = {5, 2, 3, 1};
        //           0  1  2  3
        //定义一个循环控制每轮比较的轮数
        for (int i = 1; i <= arr.length; i++) {
            //i == 1 比较的次数 3 j = 0 1 2
            //i == 2 比较的次数 2 j = 0 1
            //i == 3 比较的次数 1 j = 0
            //定义一个循环控制每轮比较的次数,占位
            for (int j = 0; j < arr.length - 1; j++) {
                //判断j当前位置的元素值 是否 大于后一个位置 若较大 则交换
                if (arr[j]>arr[j+1]){
                    int temp = arr[j+1];
                    arr[j+1]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        //遍历数组内容输出
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
    }
}

14.计算1-n的和返回

需求:定义一个方法,方法中计算出1-n的和并返回

分析:

1.根据格式编写方法-------因n不固定,故方法需要声明形参接受;要返回结果,还需申明返回值类型

2.方法内部使用for循环计算出1-n的和并返回

public class Test1 {
    public static void main(String[] args) {
        System.out.println("1-5的和是" + sum(5));
    }
    public static int sum(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
}

15.判断整数是奇数还是偶数

需求:拿一个整数,然后调用方法,把整数交给方法,在方法中输出该数为奇数还是偶数

分析:

1.根据格式编写方法--------因要传入数据给方法,方法需要声明形参接收

2.方法内部使用if语句判断,并输出对应的结论

public class Test2 {
    public static void main(String[] args) {
        check(15);
        System.out.println("--------");
        check(20);
    }
    public static void check(int number) {
        if (number % 2 == 0) {
            System.out.println(number + "是偶数");
        } else {
            System.out.println(number + "是奇数");
        }
    }
}

16.数组求最值案例改方法形式

需求:把找出数组的最大值案例,改造成方法,可以支持返回任意整形数组的最大值数据

分析:

1.根据格式编写方法

  • 要返回最大值,需要申明返回值类型
  • 需要接收数组,需要申明形参列表

2.方法内部找出数组的最大值并返回

public class Test3 {
    public static void main(String[] args) {
        int[] ages = {23, 19, 55, 89, 34};
        int max = getArrayMaxData(ages);
        System.out.println("最大值数据是" + max);

    }
    public static int getArrayMaxData(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }
}

17.打印数组内容

需求:设计一个方法用于输出任意整型数组的内容,要求输出成如下格式:“该数组内容为:[11,22,33,44,55]”

分析:

1.定义一个方法,要求该方法能够接收数组,并输出数组内容--------需要参数吗?需要返回值申明类型吗?

2.定义一个静态初始化的数组,调用该方法,并传入该数组

public class Test {
    public static void main(String[] args) {
        int[] ages = {11, 22, 33, 44, 55};
        printArray(ages);
        System.out.println("----------");
        int[] numbers = {1, 2, 3, 4, 5};
        printArray(numbers);

    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        if (arr != null && arr.length > 0) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
            }
        }
        System.out.println("]");
    }
}

18.从数组中查询指定元素的索引

需求:设计一个方法可以接收整型数组,和要查询的元素值,最终要返回元素在该数组中的索引,如果数组不存在该元素则返回-1

分析:

1.定义方法,接收整型数组,查询的元素值,在方法体中完成元素查询的功能。------是否需要参数?是否需要返回值类型?

2.定义数组,调用该方法,并指定要搜索的元素值,得到返回的结果输出

public class Test4 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 55, 34, 8};
        int index = searchIndex(arr, 11);
        System.out.println("您查询的数据索引是: " + index);
    }

    public static int searchIndex(int[] arr, int date) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == date) {
                return i;
            }
        }
        return -1;
    }
}

19.比较两个数组是否一样

需求:如果两个数组的类型,元素个数,元素顺序和内容是一样的,我们就认为这2个数组是一模一样的。请使用方法完成:能够判断任意两个整型数组是否一样,并返回true或者false。

分析:

1.定义方法,接收两个整型数组-------是否需要参数?返回值类型?

2.在方法内部完成判断逻辑,并返回布尔结果

public class Test5 {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4};
        int[] arr2 = {2, 3, 4};
        System.out.println(compare(arr1, arr2));
    }

    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;
        }
    }
}

20.买飞机票

需求:

  • 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱
  • 机票最终优惠价格的计算方案如下:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月-来年4月)头等舱7折,经济舱6.5折

分析:

  • 键盘录入机票的原价,仓位类型,月份信息,调用方法返回机票最终的优惠价格
  • 方法内部应该先使用if分支判断月份是旺季还是淡季,然后使用switch分支判断是头等舱还是经济舱
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入机票原价:");
        double money = sc.nextInt();

        System.out.println("请您输入机票的月份(1-12):");
        int month = sc.nextInt();

        System.out.println("请您选择仓位类型:");
        String type = sc.next();

        System.out.println("机票优惠后的价格是:"+calc(money,month,type));
    }

    public static double calc(double money, int month, String type) {
        if (month >= 5 && month <= 10) {
            //旺季
            switch (type) {
                case "头等舱":
                    money *= 0.9;
                    break;
                case "经济舱":
                    money *= 0.85;
                    break;
                default:
                    System.out.println("您输入的仓位类型有问题!");
                    money = -1;
            }
        } else if (month == 11 || month == 12 || month >= 1 && month <= 4) {
            //淡季
            switch (type) {
                case "头等舱":
                    money *= 0.7;
                    break;
                case "经济舱":
                    money *= 0.65;
                    break;
                default:
                    System.out.println("您输入的仓位类型有问题!");
                    money = -1;
            }
        } else {
            System.out.println("您输入的月份有错误");
            money=-1;
        }
        return money;
    }
}

21.找素数

需求:判断101-200之间有多少个素数,并输出所有素数

素数:除了1和它本身以外,不能被其他正整数整除,就叫素数

分析:

  • 101-200之间的数据可以采用循环依次拿到;每拿到一个数,判断该数是否是素数。
  • 判断规则:从2开始遍历到该数的一半的数据,看是否有数据可以整除它,有则不是素数,没有则是素数
public class Test2 {
    public static void main(String[] args) {
        for (int i = 101; i <= 200; i++) {
            boolean flag = true;
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                System.out.print(i + "\t");
            }
        }
    }
}

22.开发验证码

需求:定义一个方法,实现随机产生一个5位的验证码,每位可以是数字,大写字母,小写字母

分析:

1. 定义一个方法,生成验证码返回:方法参数是位数,方法的返回值类型是String

2.在方法内部使用for循环生成指定位数的随机字符,并连接起来

3.把连好的随机字符作为一组验证码进行返回

public class Test3 {
    public static void main(String[] args) {
        String code = createCode(5);
        System.out.println("随机验证码: " + code);

    }

    public static String createCode(int n) {
        String code = "";
        Random r = new Random();
        for (int i = 0; i < n; i++) {
            int type = r.nextInt(3);
            switch (type) {
                case 0:
                    //大写字符65-
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    //小写字符97-
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}

23.数组元素复制

需求:把一个数组中的元素复制到另一个新数组中去

分析:

  • 需要动态初始化一个数组,长度与原数组一样
  • 遍历原数组的每个元素,依次赋值给新数组
  • 输出两个数组的内容
public class Test4 {
    public static void main(String[] args) {
        int[] arr1 = {11, 22, 33, 44};
        int[] arr2 = new int[arr1.length];
        copy(arr1, arr2);
        printArray(arr1);
        printArray(arr2);
    }

    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) {
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];

        }
    }
}

24.评委打分

需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0-100]之间的整数,选手的最后得分为:去掉最高分,最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。

分析:

1.把6个评委的分数录入到程序中去------使用数组

2.遍历数组中的每个数据,进行累加求和,并找出最高分,最低分

3.按照分数的计算规则算出平均分

public class Test5 {
    public static void main(String[] args) {
        int[] scores = new int[6];
        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];
        }
        double result = (sum - max - min) * 1.0 / (scores.length - 2);
        System.out.println("选手最终得分是" + result);
    }
}

25.数字加密

需求:某系统的数字密码:比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字进行反转,得到一串新数

分析:

  • 将每位数据存入到数组中去,遍历数组每位数据按照规则进行更改,把更改后的数据重新存到数组中去
  • 将数组的前后元素进行交换,数组中的最终元素就是加密后的结果
public class Test6 {
    public static void main(String[] args) {
        System.out.println("请您输入需要加密的数字个数:");
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        int[] arr = new int[length];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请您输入加密的第" + (i + 1) + "个数字");
            int number = sc.nextInt();
            arr[i] = number;
        }
        printArray(arr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (arr[i] + 5) % 10;
        }
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
        printArray(arr);
    }

    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("]");
    }
}

26.模拟双色球

1.业务分析、随机生成一组中奖号码

        投注号码由6个红色球号码和一个蓝色球号码组成。红色球号码从1-33中选择,蓝色球号码从1-16中选择

随机一组中奖号码的分析:

  • 中奖号码由6个红球和1个蓝球组成(注意:6个红球要求不能重复)
  • 可以定义方法用于返回一组中奖号码(7个数据),返回的形式是一个整型数组

2.用户输入一组双色球号码

3.判断中奖情况

27.面向对象综合案例----模仿电影信息展示

需求:使用面向对象编程,模仿电影信息的展示

分析:

  • 一部电影是一个Java对象,需要先设计电影类,再创建电影对象
  • 三部电影对象可以采用数组存储起来
  • 依次遍历数组中的每个电影对象,取出其信息进行展示
public class Movie {
    private String name;
    private double score;
    private String actor;

    public Movie() {
    }

    public Movie(String name, double score, String actor) {
        this.name = name;
        this.score = score;
        this.actor = actor;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }
}
public class Test {
    public static void main(String[] args) {

        Movie[] movies = new Movie[3];
        movies[0] = new Movie("《长津湖》",9.7,"吴京");
        movies[1] = new Movie("《我和我的父辈》",9.6,"吴京");
        movies[2] = new Movie("《扑水少年》",9.5,"王川");

        for (int i = 0; i < movies.length; i++) {
            Movie m = movies[i];
            System.out.println("电影名称:"+m.getName());
            System.out.println("得分:"+m.getScore());
            System.out.println("主演:"+m.getActor());
            System.out.println("------------------");
        }
    }
}

28.String类开发验证码功能

需求:随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母

分析:

1.定义一个String类型的变量存储a-z A-Z 0-9 之间的全部字符

2.循环5次,随机一个范围内的索引,获取对应字符连接起来即可

public class Test8 {
    public static void main(String[] args) {
        String datas = "abcdefghizklmnopqrstuvwxyzABCDEFGHIZKLMNOPQRSTUVWXYZ012345";
        Random r = new Random();
        String code = "";
        for (int i = 0; i < 5; i++) {
            int index = r.nextInt(datas.length());
            char c = datas.charAt(index);
            code += c;
        }
        System.out.println(code);
    }
}

29.模拟用户登录功能

需求:模拟用户登录功能,最多只给三次机会

分析:

1.系统后台定义好正确的登录名称、密码

2.使用循环控制三次,让用户输入正确的用户名和密码,判断是否登录成功,登录成功则不再进行登录,登录失败给出提示,并让用户继续登录

public class Test9 {
    public static void main(String[] args) {
        String okLoginName = "admin";
        String okPassword = "123456";
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请您输入登录名称");
            String loginName = sc.next();
            System.out.println("请您输入登录密码");
            String password = sc.next();
            if (okLoginName.equals(loginName)){
                if (okPassword.equals(password)){
                    System.out.println("登录成功");
                    break;
                }else {
                    System.out.println("密码错误!还剩"+(3-i)+"次机会");
                }
            }else {
                System.out.println("用户名错误!还剩"+(3-i)+"次机会");
            }
        }
    }
}

30.手机号码屏蔽

需求:以字符串的形式从键盘接受一个手机号,将中间四位号码屏蔽

public class Test10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入您的手机号码:");
        String tel = scanner.next();

        //截取前三位,后四位
        String before = tel.substring(0, 3);
        String after = tel.substring(7);
        String s = before + "****" + after;
        System.out.println(s);
    }
}

31.遍历并删除元素值

需求:

  • 某个班级的考试在系统上进行,成绩大致分为98,77,66,89,79,50,100
  • 现在需要先把成绩低于80分以下的数据去掉

分析:

  • 定义ArrayList集合存储多名学员的成绩
  • 遍历集合每个元素,如果元素值低于80分,去掉它
public class ArrayListTest1 {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(98);
        scores.add(77);
        scores.add(66);
        scores.add(89);
        scores.add(79);
        scores.add(50);
        scores.add(100);
        System.out.println(scores);

        for (int i = 0; i < scores.size(); i++) {
            int score = scores.get(i);
            if (score < 80) {
                scores.remove(i);
                i--;
            }
        }
        System.out.println(scores);
    }
}

32.存储自定义类型的对象---影片信息在程序中的表示

需求:某影院系统需要在后台存储三部电影,然后依次展示出来

分析:

1.定义一个电影类,定义一个集合存储电影对象

2.创建三个电影对象,封装相关数据,把3个对象存入到集合中去

3.遍历集合中的3个对象,输出相关信息

public class ArrayListTest2 {
    public static void main(String[] args) {
        Movie m1 = new Movie("《肖申克的救赎》",9.7,"罗宾斯");
        Movie m2 = new Movie("《霸王别姬》",9.6,"张国荣,张丰毅");
        Movie m3 = new Movie("《阿甘正传》",9.5,"汤姆.汉克斯");
        ArrayList<Movie> movies = new ArrayList<>();
        movies.add(m1);
        movies.add(m2);
        movies.add(m3);
        for (int i = 0; i < movies.size(); i++) {
            Movie m = movies.get(i);
            System.out.println("电影名称: "+m.getName());
            System.out.println("电影得分: "+m.getScore());
            System.out.println("电影主演: "+m.getActor());
            System.out.println("----------------------");
        }
    }
}

33.元素搜索---学生信息系统的数据搜索

需求:后台程序需存储学生信息并展示,然后要提供按学号搜索学生信息的功能

分析:

1.定义Student类,定义ArrayList集合存储学生对象信息,并遍历出来

2.提供一个方法,可以接收ArrayList集合,和要搜索的学号,返回搜索到的学生信息,并展示

3.使用死循环,让用户可以不停的搜索

public class Student {
    private String studyNumber;
    private String name;
    private int age;
    private String className;

    public Student() {
    }

    public Student(String studyNumber, String name, int age, String className) {
        this.studyNumber = studyNumber;
        this.name = name;
        this.age = age;
        this.className = className;
    }

    public String getStudyNumber() {
        return studyNumber;
    }

    public void setStudyNumber(String studyNumber) {
        this.studyNumber = studyNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}
public class Test {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("20180302", "叶孤城", 23, "护理一班"));
        students.add(new Student("20180303", "东方不败", 23, "推拿二班"));
        students.add(new Student("20180304", "西门吹雪", 26, "中药学四班"));
        students.add(new Student("20180305", "梅超风", 26, "神经科二班"));
        System.out.println("学号\t名称\t年龄\t班级");
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            System.out.println(s.getStudyNumber() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassName());
        }
        //按照学号搜索功能
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入学号查询学生对象");
            String studentNumber = sc.next();
            Student s = getStudentById(students, studentNumber);
            if (s != null) {
                System.out.println(s.getStudyNumber() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassName());
            } else {
                System.out.println("查无此人");
            }
        }
    }

    /**
     * 根据学生的学号查询学生对象赶回
     *
     * @param students    存储全部学生对象的集合
     * @param studyNumber 搜索的学生的学号
     * @return 学生对象|null
     */
    public static Student getStudentById(ArrayList<Student> students, String studyNumber) {
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            if (s.getStudyNumber().equals(studyNumber)) {
                return s;
            }
        }
        return null;
    }
}

34.

  • 5
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解Java基础案例7-3日记本。这个案例是一个简单的Java GUI应用程序,可以让用户创建、查看和编辑日记条目。以下是该程序所包含的一些主要功能: 1. 创建新的日记条目:用户可以在程序中创建新的日记条目,输入标题和内容。 2. 查看日记条目列表:程序可以显示所有已创建的日记条目的列表,用户可以通过点击列表中的条目来查看它们的详细内容。 3. 编辑日记条目:用户可以编辑现有的日记条目,包括更改标题和内容。 4. 删除日记条目:用户可以删除现有的日记条目。 下面是该程序的一些关键代码片段: ``` public class DiaryGUI extends JFrame { // ... GUI component declarations ... public DiaryGUI() { // ... GUI initialization code ... // Set up action listeners for buttons newButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { createNewEntry(); } }); viewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { viewSelectedEntry(); } }); editButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { editSelectedEntry(); } }); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deleteSelectedEntry(); } }); } // Method to create a new diary entry private void createNewEntry() { // ... code to prompt user for new entry title and content ... DiaryEntry newEntry = new DiaryEntry(title, content); // ... code to add new entry to list of entries ... } // Method to view a selected diary entry private void viewSelectedEntry() { // ... code to get selected entry from list ... // ... code to display selected entry in a new window ... } // Method to edit a selected diary entry private void editSelectedEntry() { // ... code to get selected entry from list ... // ... code to prompt user for new entry title and content ... selectedEntry.setTitle(title); selectedEntry.setContent(content); } // Method to delete a selected diary entry private void deleteSelectedEntry() { // ... code to get selected entry from list ... // ... code to remove selected entry from list ... } } ``` 以上是该程序的一些基本代码片段,它们提供了创建、查看、编辑和删除日记条目的功能。当然,这个程序还有很多细节可以完善,比如数据的持久化和数据的输入验证等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值