JavaSE(基础练习题)

                                 

目录

                                                  

                                【Scanner Random if loop】

1.

   1.商场根据消费金额不同,折扣也不同,折扣的规则如下(P代表消费的总金额)        P >= 2000 7折        1000 <= P < 2000 8折        500 <= P < 1000 9.5折        P < 500 不打折    2.要求根据输入的消费金额,输出对应的折扣和折扣前后的金额

2. 

    1.模拟计算器,提示用户选择运算方式,[1加法/2减法/3乘法/4除法/5取余/0退出程序]    2.提示用户分别录入两个整数,然后根据运算方式进行计算    3.打印用户选择的运算方式,和运算结果(使用字符串拼接算式和结果)

3.

    1.使用随机数生成一个[1-100]的整数,提示用户猜一个整数    2.根据用户猜的整数,与随机数比较判断,打印用户猜的结果(猜对了或猜错了)    3.多次游戏,猜对了停止游戏

4.

    1.获取一个[10-100]之间的int类型随机数num,统计[10-num]之间,奇数的个数    2.最终在控制台打印所有的奇数,和个数

5.

    1.打印[1-1000]之间的整数,数字中包含5,或者是5的倍数的数字要跳过    2.要求控制台每行打印6个满足条件的数,之间用空格分隔

6.

    1.中国有闰年的说法。闰年的规则是:四年一闰,百年不闰,四百年再闰(年份能够被4整除但不能被100整除算是闰年,年份能被400整除也是闰年)    2.请打印出1988年到2019年的所有闰年年份

7.

    1.完成逢七过游戏    2.获取范围[1-100]的整数,如果是7的倍数的、或者包含7的要跳过,打印满足条件的数字

8.

    1.按照从大到小的顺序输出四位数中的:个位+百位=十位+千位 的数字及个数    2.每行输出5个满足条件的数,之间用空格分隔        如:9999 9988 9977 9966 9955 

                     【Array method String】

1.

    1.控制台提示并键盘录入,总共6个评委给1名选手打分,分数为[1-100]之间的int类型整数    2.对录入分数进行判断,超出范围给出提示,重新录入    4.将分数存储到合适的数组中    3.去除一个最高分、一个最低分,统计该选手的平均成绩,并在控制台打印

2.

    1.定义一个长度为5的int类型数组arr,数组元素为[7-67]之间的随机整数    2.遍历数组arr,打印所有元素    3.提示用户输入[3-5]之间的一个整数num,输入数字不需要判断    4.筛选出数组arr中是num倍数的元素,并打印在控制台

3.

    1.定义一个方法,该方法能在一个int类型数组中查询某元素num是否存在,并返回被查找元素的索引    2.main方法中定义一个数组arr={19,28,37,46,50}; 提示并使用键盘录入一个整数    3.调用方法,查询该整数是否在arr数组中,如果在打印索引,如果不在给出提示 

4.

    1.定义一个方法getSum(int start, int end){ … },该方法完成获取[start-end]范围中的数据的和    2.在main方法中提示,并键盘录入start和end(start小于end,需要判断),调用方法传递参数,打印结果

5.

    1.定义一个方法,实现将指定int数组中的索引为偶数的元素值增加1    2.假如数组为{1,2,3,4,5,6,7},那么调用方法后,数组中的元素变为{2,2,4,4,6,6,8}    3.main方法中,调用方法测试结果

6.

    1.定义方法getMax()获取数组最大值、getMin()获取最小值、getAvg()获取元素平均值    2.在main方法中,生成一个长度为10的随机数组arr,范围是[1-100]    3.分别调用三个方法,传递该数组,接收返回值并打印

7.

    1.定义一个方法getCount(int[] arr) ,获取一个数组中,小于平均数的元素个数并返回    2.在main方法中,生成一个长度为8的随机数组arr,范围是[5,65]    3.调用getCount()方法,传递数组,接收并打印结果

8.

    1.定义一个方法,实现交换int数组中,两个索引上的元素值,并调用方法,查看运行效果    例如,数组为{11,22,33,44,55,66},交换索引1和5位置上的元素,结果为{11,66,33,44,55,22}

9.

    1.定义一个方法,求一个字符串的长度,不能使用String的length()方法    2.在main函数中输入字符串,并输出其长度

10.

    1.提示用户键盘录入一个包含数字和字母的字符串(不用做是否包含数字和字母的判断)    2.遍历字符串,分别筛选出数字和字母,按照数字在前字母在后的规则,组成一个新的字符串,并打印

11.

    1.提示用户输入手机号码    2.通过代码逻辑进行修改,要求中间四位使用*代替    示例:用户输入13279440986 控制台打印:132****0986

12.

    1.控制台模拟对话框,提示用户输入聊天内容    2.对敏感词汇进行替换    要求:将TMD替换为***

13.

    1.提示用户录入一个字符串,要求字符串中必须存在字母(可以定义方法)    2.不符合要求:提示录入有误!重新录入    3.符合要求:判断字符串中大写字母出现次数并打印(可以定义方法)

14.

    1.循环录入班级学员Java课程的成绩(学员数量由键盘录入)    2.统计分数大于等于80分的学生的比例

15.

    1.假设有一个长度为5的数组,数组元素通过键盘录入,如下所示:        int[] arr = {1,3,-1,5,-2};    2.要求:        创建一个新数组newArr[]        新数组中元素的存放顺序与原数组中的元素逆序        并且如果原数组中的元素值小于0在新数组中按0存储        最后输出原数组和新数组中的内容

16.

   1.请用户输入一个String类型的电话号码,定义方法来校验这个号码是否正确    2.要求:        必须以数字1开头        必须是11位数字


                

                       【Scanner Random if loop】

1.

   1.商场根据消费金额不同,折扣也不同,折扣的规则如下(P代表消费的总金额)
        P >= 2000 7折
        1000 <= P < 2000 8折
        500 <= P < 1000 9.5折
        P < 500 不打折
    2.要求根据输入的消费金额,输出对应的折扣和折扣前后的金额

public static void main(String[] args) {

        // 创建对象
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入消费金额:");
        int p = sc.nextInt(); // 定义p用于从键盘接收商品价格

        if (p >= 2000) {
            System.out.println("本次消费:" + p + "元!");
            System.out.println("您可享受7折优惠!");
            System.out.println("您需支付:" + p * 0.7 + "元!");
        }if (p>=1000 && p<=2000){
            System.out.println("本次消费:" + p + "元!");
            System.out.println("您可享受8折优惠!");
            System.out.println("您需支付:" + p * 0.8 + "元!");
        }if (p>=500 && p<=1000){
            System.out.println("本次消费:" + p + "元!");
            System.out.println("您可享受9.5折优惠!");
            System.out.println("您需支付:" + p * 0.95 + "元!");
        }if (p<500){
            System.out.println("本次消费:" + p + "元!");
            System.out.println("不打折!");
            System.out.println("您需支付:" + p  + "元!");
        }

    }

2. 

    1.模拟计算器,提示用户选择运算方式,[1加法/2减法/3乘法/4除法/5取余/0退出程序]
    2.提示用户分别录入两个整数,然后根据运算方式进行计算
    3.打印用户选择的运算方式,和运算结果(使用字符串拼接算式和结果)

public static void main(String[] args) {

        // 提示
        System.out.println("**********计算器**********");
        System.out.println("---------[1] 加法--------");
        System.out.println("---------[2] 减法--------");
        System.out.println("---------[3] 乘法--------");
        System.out.println("---------[4] 除法--------");
        System.out.println("---------[5] 取余--------");
        System.out.println("---------[0] 退出程序-----");



        while (true){
            System.out.println("如要退出请按[0]!");
            System.out.println("请选择需要进行的操作[1-5]:");

            // 定义num接收需要操作的序号
            int num = new Scanner(System.in).nextInt();
        if (num == 0){
            System.out.println("-------------------- -感谢您的使用--------------------------");
            System.out.println("              .::::.");
            System.out.println("            .::::::::.");
            System.out.println("           :::::::::::");
            System.out.println("        ..:::::::::::'");
            System.out.println("      '::::::::::::'");
            System.out.println("        .::::::::::");
            System.out.println("   '::::::::::::::..");
            System.out.println("        ..::::::::::::.");
            System.out.println("      ``::::::::::::::::");
            System.out.println("       ::::``:::::::::'        .:::.");
            System.out.println("      ::::'   ':::::'       .::::::::.");
            System.out.println("    .::::'      ::::     .:::::::'::::.");
            System.out.println("  .:::'         :::::  .:::::::::' ':::::.");
            System.out.println(".::'            :::::.:::::::::'      ':::::.");
            System.out.println(".::'            ::::::::::::::'         ``::::.");
            System.out.println("...:::           ::::::::::::'              ``::.");
            System.out.println("````':.          ':::::::::'                  ::::..");
            System.out.println("                  '.:::::'                    ':'````..");
            System.out.println("------------------------------------------------------------");
            System.exit(0);
        }
        // 定义num1 和 num2 用来区分需要操作的数
        System.out.println("请输入第一个数:");
        int num1 = new Scanner(System.in).nextInt();
        System.out.println("请输入第二个数:");
        int num2 = new Scanner(System.in).nextInt();


        switch (num) {
            case 1:
                int a = num1 + num2;
                System.out.println(num1 + "+" + num2 + "=" + a);
                break;
            case 2:
                int b = num1 - num2;
                System.out.println(num1 + "-" + num2 + "=" + b);
                break;
            case 3:
                int c = num1 * num2;
                System.out.println(num1 + "*" + num2 + "=" + c);
                break;
            case 4:
                int d = num1 / num2;
                System.out.println(num1 + "/" + num2 + "=" + d);
                break;
            case 5:
                int e = num1 % num2;
                System.out.println(num1 + "%" + num2 + "=" + e);
                break;
        }
        }
    }

3.

    1.使用随机数生成一个[1-100]的整数,提示用户猜一个整数
    2.根据用户猜的整数,与随机数比较判断,打印用户猜的结果(猜对了或猜错了)
    3.多次游戏,猜对了停止游戏

 public static void main(String[] args) {
        // 创建 随机数
        int r = new Random().nextInt(100) + 1;

        // 进入循环,当猜中结果时跳出循环
        while (true) {
            // 控制台输出提示并录入键盘输入数据
            System.out.println("请输入您猜想的数字:");
            int num = new Scanner(System.in).nextInt();
            // 健壮性判断
            if (r<1 || r>100){
                System.out.println("您输入的数据有误,请重新输入!");
            }else if (r == num) {
                System.out.println("恭喜您,猜对了!");
                break;
            } else if (num > r) {
                System.out.println("猜大了,请继续努力");
            } else {
                System.out.println("猜小啦,请继续努力!");
            }
        }
    }

4.

    1.获取一个[10-100]之间的int类型随机数num,统计[10-num]之间,奇数的个数
    2.最终在控制台打印所有的奇数,和个数

public static void main(String[] args) {

        int count = 0; // 计数器

        // 创建变量num 接收生成的随机数
        int num = new Random().nextInt(91) + 10;

        System.out.println("[10-"+num+"]之间的奇数为:");
        // 用for循环来遍历 10 - num之间的数
        for (int i = 10; i <= num; i++) {
            if (i % 2 != 0) {
                System.out.print( i+" ");
                count++;
            }
        }
        System.out.println();
        System.out.println("奇数的个数为:" + count + "个.");
    }

5.

    1.打印[1-1000]之间的整数,数字中包含5,或者是5的倍数的数字要跳过
    2.要求控制台每行打印6个满足条件的数,之间用空格分隔

public static void main(String[] args) {

        int count = 0;
        System.out.println("数字中包含5,或者是5的倍数的数字为:");
        // 利用for循环遍历1-1000
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0 || i / 10 == 5 || i / 100 == 5) {
                continue;
            }
            System.out.print(i + " ");

            count++;
            if (count%6==0)
                System.out.println();
        }
    }

6.

    1.中国有闰年的说法。闰年的规则是:四年一闰,百年不闰,四百年再闰(年份能够被4整除但不能被100整除算是闰年,年份能被400整除也是闰年)
    2.请打印出1988年到2019年的所有闰年年份

public static void main(String[] args) {

        for (int i = 1988; i < 2000; i++) {
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                System.out.println(i + " ");
            }
        }
    }

7.

    1.完成逢七过游戏
    2.获取范围[1-100]的整数,如果是7的倍数的、或者包含7的要跳过,打印满足条件的数字

public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {

            if (i % 7 != 0 && i % 10 != 7 && i / 10 % 10 != 7 && i / 100 != 7){
                System.out.print(i+" ");
            }
        }
    }

8.

    1.按照从大到小的顺序输出四位数中的:个位+百位=十位+千位 的数字及个数
    2.每行输出5个满足条件的数,之间用空格分隔
        如:9999 9988 9977 9966 9955 

public static void main(String[] args) {

        int count = 0;
        for (int i = 1000; i <=9999 ; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            int qian = i / 1000;
            if (ge+shi == bai + qian){
                System.out.print(i+" ");
                count++;
                if (count % 5==0){
                    System.out.println();
                }
            }
        }
        System.out.println("满足条件的数字共有:"+count+"个.");
    }

                     【Array method String】

1.

    1.控制台提示并键盘录入,总共6个评委给1名选手打分,分数为[1-100]之间的int类型整数
    2.对录入分数进行判断,超出范围给出提示,重新录入
    4.将分数存储到合适的数组中
    3.去除一个最高分、一个最低分,统计该选手的平均成绩,并在控制台打印

public static void main(String[] args) {
        int[] arr = new int[6];
        int max = arr[0];         // 假设第一个评委给的分数为最高分
        int min = 0;          // 用于接收最小分数
        int sum = 0;          // 分数之和
        // 提示
        System.out.println("请各位评委打分! 注:分数在1-100之间!");
        Scanner sc = new Scanner(System.in);
        // 定义数组arr用于接收键盘录入分数
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请第" + (i + 1) + "位评委打分:");
            arr[i] = sc.nextInt();
            // 判断输入分数是否合法
            if (arr[i] > 100 || arr[i] < 0) {
                System.out.println("输入分数有误,头打烂....重新输入!!!");
                i--;
            }
        }

        // 遍历查询出最高分
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        int Max = max;
        System.out.println("去除一个最高分:" + Max);

        // 遍历查询出最低分
        for (int i = 0; i < arr.length ; i++) {
            if (arr[i] < max) {
                max = arr[i];
            }
             min = max;
        }
        System.out.println("去除一个最低分:" + min);

        // 计算平均成绩
        for (int i = 0; i < arr.length; i++) {
            sum+=arr[i];
        }
        int result = (sum - (Max+min))/arr.length;   // 最终结果
        System.out.println("最终得分:"+result);
    }

2.

    1.定义一个长度为5的int类型数组arr,数组元素为[7-67]之间的随机整数
    2.遍历数组arr,打印所有元素
    3.提示用户输入[3-5]之间的一个整数num,输入数字不需要判断
    4.筛选出数组arr中是num倍数的元素,并打印在控制台

public static void main(String[] args) {

        // 1.定义一个长度为5的int类型数组arr,数组元素为[7-67]之间的随机整数
        int[] arr = new int[5];
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int j = r.nextInt(60) + 7;
            arr[i] = j;
        }
        // 2.遍历数组打印
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        // 3.提示用户输入[3-5]之间的一个整数num,输入数字不需要判断
        System.out.println("请输入3-5之间的任何一个整数:");
        int num = new Scanner(System.in).nextInt();

        // 4.筛选出数组arr中是num倍数的元素,并打印在控制台
        System.out.println("数组中"+num+"的倍数是为:");
        for (int i = 0; i <arr.length ; i++) {
            if (arr[i] % num ==0){
                System.out.print(arr[i] +" ");
            }
        }
    }

3.

    1.定义一个方法,该方法能在一个int类型数组中查询某元素num是否存在,并返回被查找元素的索引
    2.main方法中定义一个数组arr={19,28,37,46,50}; 提示并使用键盘录入一个整数
    3.调用方法,查询该整数是否在arr数组中,如果在打印索引,如果不在给出提示 

public static void main(String[] args) {
        //     2.main方法中定义一个数组arr={19,28,37,46,50}; 提示并使用键盘录入一个整数
        int[] arr = {19, 28, 37, 46, 50};
        System.out.println("请输入一个整数:");
        int num = new Scanner(System.in).nextInt();
        demo(num, arr);     // 调用demo方法,传入数据
    }

    // 1.定义一个方法,该方法能在一个int类型数组中查询某元素num是否存在,并返回被查找元素的索引
    public static void demo(int num, int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                System.out.println("查找成功,索引为:" + i);
                count++;
            }
        }
        if (count == 0) {
            System.out.println("您输入的数字在该数组中未找到!");
        }
    }

4.

    1.定义一个方法getSum(int start, int end){ … },该方法完成获取[start-end]范围中的数据的和
    2.在main方法中提示,并键盘录入start和end(start小于end,需要判断),调用方法传递参数,打印结果

public static void main(String[] args) {
        // 提示输入数据
        System.out.println("请输入第一个数字:");
        int num1 = new Scanner(System.in).nextInt();
        System.out.println("请输入第二个数字:");
        int num2 = new Scanner(System.in).nextInt();

        getSum(num1, num2);  // 调用getSum方法
    }

    public static void getSum(int start, int end) {
        int sum = 0;
        // 健壮性判断
        if (end < start) {
            System.out.println("您输入的数据有误,请重新输入");
        } else  {
            for (int i = start; i<= end; i++) {
                sum += i;
            }
            System.out.println(start + "到" + end + "之间的数字和为:" + sum);
        }
    }

5.

    1.定义一个方法,实现将指定int数组中的索引为偶数的元素值增加1
    2.假如数组为{1,2,3,4,5,6,7},那么调用方法后,数组中的元素变为{2,2,4,4,6,6,8}
    3.main方法中,调用方法测试结果

public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        demo(arr);
    }

    public static void demo(int[] arr) {
        // 判断数组的索引是否为偶数,如果是则+1
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 == 0) {
                arr[i] += 1;
            }
        }
        // 遍历数组并输出
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i] + " ");
        }
    }

6.

    1.定义方法getMax()获取数组最大值、getMin()获取最小值、getAvg()获取元素平均值
    2.在main方法中,生成一个长度为10的随机数组arr,范围是[1-100]
    3.分别调用三个方法,传递该数组,接收返回值并打印

public static void main(String[] args) {
        // 采用随机数生成长度为10的数组
        int[] arr = new int[10];
        for (int i = 0;i<arr.length;i++){
            arr[i]=new Random().nextInt(100)+1;
        }
        getMax(arr);
        getMin(arr);
        getAvg(arr);
    }
    
    // 获取数组最大值    采用冒泡排序
    public static void getMax(int[] arr){
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i-1 ; j++) {
                if (arr[j]<arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        System.out.println("最大值为:"+arr[0]);
    }
    // 获取数组最小值   采用冒泡排序
    public static void getMin(int[] arr){
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i-1 ; j++) {
                if (arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        System.out.println("最小值为:" + arr[0]);
    }
    //获取元素平均值
    public static void getAvg(int[] arr){
        int sum = 0;
        for (int i =0 ;i<arr.length;i++){
            sum += arr[i];
        }
        int avg = sum/arr.length;
        System.out.println("平均值为:" + avg);
    }

7.

    1.定义一个方法getCount(int[] arr) ,获取一个数组中,小于平均数的元素个数并返回
    2.在main方法中,生成一个长度为8的随机数组arr,范围是[5,65]
    3.调用getCount()方法,传递数组,接收并打印结果

public static void main(String[] args) {
        // 定义生成数组,并生成随机数组
        int[] arr = new int[8];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Random().nextInt(61) + 5;
        }
        // 调用方法
        getCount(arr);   
    }

    public static void getCount(int[] arr) {

        int count = 0;    // 计数
        int sum = 0;      // 求和

        // 循环遍历数组,求和并计算平均值
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }

        int avg = sum / arr.length;

        // 遍历数组,判断是否大于平均数
        System.out.println("小于平均数的元素为:");
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < avg) {
                System.out.print(arr[i] + " ");
                count++;
            }
        }
        System.out.println();
        System.out.println("总共有" + count + "个元素");
    }

8.

    1.定义一个方法,实现交换int数组中,两个索引上的元素值,并调用方法,查看运行效果
    例如,数组为{11,22,33,44,55,66},交换索引1和5位置上的元素,结果为{11,66,33,44,55,22}

public static void main(String[] args) {
        int[] arr= {11,22,33,44,55,66};
        exchange(arr);
    }

    // exchange()方法实现数组交换
    public static void exchange(int[] arr){

        System.out.println("交换位置前的数组为:");
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+" ");
        }

        int temp = arr[1];
        arr[1] = arr[5];
        arr[5] = temp;

        System.out.println();
        System.out.println("交换位置后的数组为:");
        for (int i = 0 ; i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }

9.

    1.定义一个方法,求一个字符串的长度,不能使用String的length()方法
    2.在main函数中输入字符串,并输出其长度

public static void main(String[] args) {
        // 键盘录入字符
        System.out.println("请输入一个字符串:");
        String s = new Scanner(System.in).nextLine();
        // 调用方法
        getCharLength(s);
    }

    // getCharLength()方法求字符串长度
    public static void getCharLength(String s) {
        // 计数器
        int count = 0;
        // 将接收的字符串转换存储到字符串数组之中
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            count++;
        }
        System.out.println("该字符串共有" + count + "个字符.");
    }

10.

    1.提示用户键盘录入一个包含数字和字母的字符串(不用做是否包含数字和字母的判断)
    2.遍历字符串,分别筛选出数字和字母,按照数字在前字母在后的规则,组成一个新的字符串,并打印

 public static void main(String[] args) {
        // 提示并从键盘录入数据
        System.out.println("请输入一个包含数字和字母的字符串:");
        String s = new Scanner(System.in).nextLine();
        
        // 方法一:使用charAt()方法返回索引值进行遍历
        // 字母字符
        String word = "";
        // 数字字符
        String num = "";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
                word += c;
            } else if (c >= '0' && c <= '9') {
                num += c;
            }
        }
        System.out.println("拼接成的新字符串为:" + num + word);
    }

11.

    1.提示用户输入手机号码
    2.通过代码逻辑进行修改,要求中间四位使用*代替
    示例:用户输入13279440986 控制台打印:132****0986

public static void main(String[] args) {
        // 1.提示用户输入手机号码
        System.out.println("请输入手机号码:");
        String tel = new Scanner(System.in).nextLine();

        // 使用substring()方法进行截取
        String start = tel.substring(0, 3);
        String end = tel.substring(7);

        System.out.println("输出显示为:"+ start + "****" + end);
    }

12.

    1.控制台模拟对话框,提示用户输入聊天内容
    2.对敏感词汇进行替换
    要求:将TMD替换为***

 public static void main(String[] args) {
        System.out.println("请输入聊天内容:");
        String s = new Scanner(System.in).nextLine();
        String tmd = s.replace("TMD", "***");
        System.out.println(tmd);
    }

13.

    1.提示用户录入一个字符串,要求字符串中必须存在字母(可以定义方法)
    2.不符合要求:提示录入有误!重新录入
    3.符合要求:判断字符串中大写字母出现次数并打印(可以定义方法)

public static void main(String[] args) {
        System.out.println("请输入一个字符串:");
        String s = new Scanner(System.in).nextLine();

        bigWord(s);
    }

    /*
    // 判断录入字符串是否存在字母 使用charAt()
    public static void bigWord(String s) {
        // 统计大写字母出现的次数
        int count = 0;
        // 循环遍历字符串并判断
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                count++;
            } else if (i == s.length()-1 && count == 0) {
                System.out.println("您输入的字符串中没有大写字母,请重新输入!");
                System.exit(0);
            }
        }
        System.out.println("大写字母一共有:" + count + "个");

     */

    // 使用 toCharArray()
    public static void bigWord(String s) {
        int count = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 'A' && chars[i] <= 'Z') {
                count++;
            } else if (i == s.length() - 1 && count == 0) {
                System.out.println("您输入的字符串中没有大写字母,请重新输入!");
                System.exit(0);
            }
        }
        System.out.println("大写字母一共有:" + count + "个");
    }

14.

    1.循环录入班级学员Java课程的成绩(学员数量由键盘录入)
    2.统计分数大于等于80分的学生的比例

public static void main(String[] args) {
        int count = 0;
        // 提示
        System.out.println("请输入学员数量:");
        // 键盘录入学院数量
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        // 定义数组接收键盘录入的成绩
        int[] arr = new int[num];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "位同学成绩:");
            arr[i] = sc.nextInt();
            // 判断输入成绩是否大于80分
            if (arr[i] >= 80) {
                count++;
            }
        }

        System.out.println("分数大于等于80分的学生约占" + count + "/" + num);
    }

15.

    1.假设有一个长度为5的数组,数组元素通过键盘录入,如下所示:
        int[] arr = {1,3,-1,5,-2};
    2.要求:
        创建一个新数组newArr[]
        新数组中元素的存放顺序与原数组中的元素逆序
        并且如果原数组中的元素值小于0在新数组中按0存储
        最后输出原数组和新数组中的内容

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[5];
        // 键盘录入5个整数
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个数:");
            arr[i] = sc.nextInt();
        }

        // 数组逆序排序
        System.out.println("原数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        /* 利用指针思想
        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();
        System.out.println("排序后:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }

         */
        // 创建新数组用于接收逆序数组
        int[] newArr = new int[5];
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] < 0) {
                arr[i] = 0;
            }
            newArr[arr.length-1-i] = arr[i];
        }

        // 遍历接收逆序数据的新数组
        System.out.println();
        System.out.println("新数组:");
        for (int i = 0; i < newArr.length; i++) {
            System.out.print(newArr[i] + " ");
        }
    }

16.

   1.请用户输入一个String类型的电话号码,定义方法来校验这个号码是否正确
    2.要求:
        必须以数字1开头
        必须是11位数字

public static void main(String[] args) {
        System.out.println("请输入需要校验的手机号码:");
        // 用户从键盘接收电话
        String tel = new Scanner(System.in).nextLine();

        char[] chars = tel.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c1 = chars[i];
            if (c1 >'9' || c1<'0'){
                System.out.println("手机号码应该为数字,您的输入有误,请重新输入!");
            }
        }
        // 获取第一个字符,进行判断
        char c = tel.charAt(0);
        if (c !='1'){
            System.out.println("手机号码第一位必须为1,您输入的号码有误,请重新输入!");
        }else if (tel.length()<11){
            System.out.println("您输入的号码位数不对,请重新输入!");
        }else {
            System.out.println("输入号码正确,感谢您的使用!");
        }
    }

  • 3
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值