JavaSE练习01-编程基础_综合练习

经过2个星期的学习,检验自己Java学习情况的练习题(附代码)

知识点

编程基础练习

题目1(综合)

通过键盘录入的方式输入星期数(1-7的整数),显示今天的减肥活动,使用switch和if两种判断语句分别完成。(可写成2个方法)
​ 周一:跑步
​ 周二:游泳
​ 周三:慢走
​ 周四:动感单车
​ 周五:拳击
​ 周六:爬山
​ 周日:好好吃一顿

public class JavaDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入1-7整数:");
        int week = sc.nextInt();
        ifweek(week);
        switchweek(week);
    }

    public static void ifweek(int day) {
        if (day==1){
            System.out.println("周一:跑步");
        } else if (day==2) {
            System.out.println("周二:游泳");
        } else if (day==3) {
            System.out.println("周三:慢走");
        } else if (day==4) {
            System.out.println("周四:动感单车");
        } else if (day==5) {
            System.out.println("周五:拳击");
        } else if (day==6) {
            System.out.println("周六:爬山");
        } else if (day==7) {
            System.out.println("周日:好好吃一顿");
        } else {
            System.out.println("输入星期有误,请重新输入(1-7)");
        }
    }
    public static void switchweek (int day) {
        switch (day){
            case 1:
                System.out.println("周一:跑步");
                break;
            case 2:
                System.out.println("周二:游泳");
                break;
            case 3:
                System.out.println("周三:慢走");
                break;
            case 4:
                System.out.println("周四:动感单车");
                break;
            case 5:
                System.out.println("周五:拳击");
                break;
            case 6:
                System.out.println("周六:爬山");
                break;
            case 7:
                System.out.println("周日:好好吃一顿");
                break;
            default:
                System.out.println("输入星期有误,请重新输入(1-7)");
        }
    }
}

题目2(综合)

​ 朋友聚会的时候可能会玩一个游戏:逢七过。
​ 规则是:从任意一个数字开始报数,当你要报的数字包含7或者是7的倍数时都要说:过。
​ 为了帮助大家更好的玩这个游戏,这里我们直接在控制台打印出1-100之间的满足逢七必过规则的数据。
​ 这样,大家将来在玩游戏的时候,就知道哪些数据要说:过。

public class JavaDemo{
    public static void main(String[] args) {
        System.out.println("1-100包含7或者是7的倍数需要过的数:");
        for (int i = 1;i <= 100; i++){
            if (i%7==0 || i%10==7 || i/10%10==7){
                System.out.println(i);
            }
        }
    }
}

题目3(综合)

有一个很有名的数学逻辑题叫做不死神兔问题。有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?

public class JavaDemo{
    public static void main(String[] args) {
        int[] arr=new int[20];
        arr[0]=1;
        arr[1]=1;
        for (int i=2;i<arr.length;i++){
            arr[i]=arr[i-1]+arr[i-2];
        }
        System.out.println(Arrays.toString(arr));
        System.out.println("第二十个月的兔子对数为"+arr[19]+"对");
    }
}

题目4(综合)

​ 我国古代数学家张丘建在《算经》一书中提出的数学问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。
​ 百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?

public class JavaDemo{
    public static void main(String[] args) {
        int cock=5;
        int hen=3;
        int chicken3=1;
        for (int i=0;i<=20;i++){
            for (int x=0;x<=33;x++){
                for (int y=0;y<=100;y++){
                    if (i+x+(y*3)==100 && (i*cock)+(x*hen)+(y*chicken3)==100){
                        System.out.println("买鸡翁"+i+"只"+","+"买鸡母"+x+"只"+","+"买鸡雏"+(y*3)+"只");
                    }
                }
            }
        }
    }
}

题目5(综合)

有这样的一个数组,元素是{68,27,95,88,171,996,51,210}。求出该数组中满足要求的元素和,
要求是:求和的元素个位和十位都不能是7,并且只能是偶数。

public class JavaDemo{
    public static void main(String[] args) {
        int[] arr={68,27,95,88,171,996,51,210};
        int sum=0;
        for (int i=0;i<arr.length;i++){
            if (arr[i]/10!=7 && arr[i]/10%10!=7 && arr[i]%2==0){
                sum+=arr[i];
            }
        }
        System.out.println("该数组中满足要求的元素和为:"+sum);
    }
}

题目6(综合)

设计一个方法,用于比较两个数组的内容是否相同。

import java.util.Arrays;
import java.util.Scanner;

public class JavaDemo {
    public static void main(String[] args) {
        int[] arr1 = new int[3];
        int[] arr2 = new int[3];
        for (int i = 0; i < arr1.length; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数组1:");
            int num1 = sc.nextInt();
            arr1[i] = num1;
        }
        for (int i = 0; i < arr2.length; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数组2:");
            int num2 = sc.nextInt();
            arr2[i] = num2;
        }
        equals(arr1, arr2);
    }

    public static void equals(int[] array1, int[] array2) {
        boolean a = false;
        for (int i = 0; i < array1.length; i++) {
            for (int x = 0; i < array2.length; x++) {
                if (array2[x] != array1[i]) {
                    a = false;
                    break;
                }else {
                    a = true;
                }
            }
        }
        if (a==true){
            System.out.println("数组1:" + Arrays.toString(array1) + "和数组2:" + Arrays.toString(array2) + "是相等的");
        }
        if (a==false){
            System.out.println("数组1:" + Arrays.toString(array1) + "和数组2:" + Arrays.toString(array2) + "是不相等的");
        }
    }
}

题目7(综合)

已知一个数组 arr = {19, 28, 37, 46, 50}; 键盘录入一个数据。定义一个方法,完成查找该数据在数组中的索引,并在控制台输出找到的索引值。

import java.util.Scanner;

public class JavaDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需查找的数:");
        int num = sc.nextInt();
        if (find(num) == -1) {
            System.out.println("查无此数");
        } else {
            System.out.println("该数据在数组中的索引值为:" + find(num));
        }
    }

    public static int find(int num) {
        int[] arr = {19, 28, 37, 46, 50};
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (num == arr[i]) {
                index = i;
            } else {
                index = -1;
            }
        }
        return index;
    }
}

题目8(综合)

​ 已知一个数组 arr = {19, 28, 37, 46, 50}; 用程序实现把数组中的元素值反转(在原数组中操作,不能定义第二个数组),反转后的数组 arr = {50, 46, 37, 28, 19}; 并在控制台输出反转后的数组元素。

public class JavaDemo {
    public static void main(String[] args) {
        int[] arr = {19, 28, 37, 46, 50};
        for (int i = arr.length-1; i >= 0 ; i--) {
            System.out.print(arr[i]+",");
        }
    }
}

题目9(综合)

在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分。
选手的最后得分为:去掉一个最高分和一个最低分后的4个评委平均值 (不考虑小数部分)。

import java.util.Scanner;

public class JavaDemo {
    public static void main(String[] args) {
        int[] arr = new int[6];
        int maxIndex = 0;
        int minIndex = 0;
        int sum = 0;
        int ave = 0;
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个评委的评分(0-100):");
            int score = sc.nextInt();
            arr[i] = score;
        }
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                maxIndex = i;
            }
        }
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
                minIndex = i;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if (i == maxIndex | i == minIndex) {
                continue;
            }
            sum += arr[i];
            ave = sum / (arr.length - 2);
        }
        System.out.println("选手的最后得分为:" + ave);
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值