20230328——java方法的案例day03

1、

package com.wanxi;

public class FindTheSeven {
    @SuppressWarnings("ConvertToBasicLatin")
    public static void main(String[] args) {
//        需求:
//        规则是:从任意一个数字开始报数,当你要报的数字包含7或者是7的倍数时都要说:过。
//        为了帮助大家更好的玩这个游戏,这里我们直接在控制台打印出1-100之间的满足逢七必过规则的数据。
//        这样,大家将来在玩游戏的时候,就知道哪些数据要说:过。
//        如何测试?
//        一组样例数据:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
//        用这组数据作为测试数据来验证自己的算法是否正确。


        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 71, 72, 77, 87, 97, 99, 124, 137, 712, 979, 1117, 1171, 1711, 7111, 7777};
        for (int i = 0; i < array.length; i++) {
            int number = array[i];

            //1、先找出7及7的倍数
            if (doubleSeven(number)) {
                System.out.println(number + "是7的倍数,跳过");
                continue;
            }
            //2、找包含7的数。
            if (containSeven(number)) {
                System.out.println(number + "包含7,跳过");
                continue;
            }


//            if (zeroToHundred(number)) {
//                //1、先找出7及7的倍数
//                if (doubleSeven(number)) {
//                    System.out.println(number + "是7的倍数,跳过");
//                    continue;
//                }
//                //2、找包含7的数。
//                if (containSeven(number)) {
//                    System.out.println(number + "包含7,跳过");
//                    continue;
//                }
//            }
//
//
//            if (hundredToThousand(number)) {
//                //1、先找出7及7的倍数
//                if (doubleSeven(number)) {
//                    System.out.println(number + "是7的倍数,跳过");
//                    continue;
//                }
//                //2、找包含7的数。
//                if (containSeven(number)) {
//                    System.out.println(number + "包含7,跳过");
//                    continue;
//                }
//            }
//
//            if (thousandToTenThousand(number)) {
//                //1、先找出7及7的倍数
//                if (doubleSeven(number)) {
//                    System.out.println(number + "是7的倍数,跳过");
//                    continue;
//                }
//                //2、找包含7的数。
//                if (containSeven(number)) {
//                    System.out.println(number + "包含7,跳过");
//                    continue;
//                }
//            }




            System.out.println(number + "报数,number: " + number);

        }

    }




//    private static boolean zeroToHundred(int number) {
//        if (number >= 0 && number < 100) {
//                return true;
//        }
//        return false;
//    }
//
//
//    private static boolean hundredToThousand(int number) {
//        if (number >= 100 && number < 1000) {
//            return true;
//        }
//        return false;
//    }
//
//
//    private static boolean thousandToTenThousand(int number) {
//        if (number >= 1000 && number < 10000) {
//            return true;
//        }
//        return false;
//    }






    private static boolean containSeven(int number) {
        int ge = number % 10;
        int shi = number / 10 % 10;
        int bai = number / 100 % 10;
        int qian = number / 1000 % 10;
        if (ge == 7 || shi == 7 || bai == 7 || qian == 7) {
            return true;
        }
        return false;
    }

    private static boolean doubleSeven(int number) {
        if (number % 7 == 0) {
            return true;
        }
        return false;
    }

}

2、

package com.wanxi;

public class TheSum {
    //需求:有这样的一个数组,元素是{68,27,95,88,171,172,996,51,210}。求出该数组中满足要求的元素和,
//       要求是:求和的元素个位和十位都不能是7,并且只能是偶数
    public static void main(String[] args) {
        int[] array = {68, 27, 95, 88, 171, 172, 177,996, 51, 210};

        int sum = 0;

        sum = getSum(array, sum);

        System.out.println(sum);

    }

    private static int getSum(int[] array, int sum) {
        for (int i = 0; i < array.length; i++) {
            int number = array[i];
            if (number % 2 == 0) {
                int ge = number % 10;
                int shi = number / 10 % 10;

                if (ge != 7 && shi != 7) {

                    sum += number;

                }

            }

        }
        return sum;
    }


}

3、

package com.wanxi;

public class CompareArray {
    public static void main(String[] args) {
        int[] arrayOne = {1, 2, 3};
        int[] arrayTwo = {1,2,3};
        arrayTwo=arrayOne;

        if (arrayOne == arrayTwo) {
            System.out.println("两个数组相同,地址相同");
            return;
        }

        if (arrayOne.length == arrayTwo.length) {
            int time = 0;
            for (int i = 0; i < arrayOne.length; i++) {
                int one = arrayOne[i];
                int two = arrayTwo[i];
                if (one == two) {
                    time++;
                }
            }
            if (time == arrayOne.length) {
                System.out.println("两个数组地址不相同,长度、内容相同");
                return;
            }
            System.out.println("两个数组地址不相同,长度相同,但内容不相同");
            return;
        }

        System.out.println("两个数组不相同");
    }
}


4、

package com.wanxi;

import java.util.Scanner;

//需求:已知一个数组 arr = {19, 28, 37, 46, 50}; 键盘录入一个数据,查找该数据在数组中的索引。
//并在控制台输出找到的索引值。如果没有查找到,则输出-1
public class FindArray {
    public static void main(String[] args) {

        int[] array = {19, 28, 37, 46, 50};
        int theNumber = getTheNumber("请输入一个整数: ");

        int indexNumber = indexNumber(array, theNumber);
        System.out.println(theNumber + "的索引为: " + indexNumber);

    }

    private static int getTheNumber(String tips) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(tips);
        int theNumber = scanner.nextInt();
        return theNumber;
    }

    private static int indexNumber(int[] array, int theNumber) {
        for (int i = 0; i < array.length; i++) {
            int number = array[i];
            if (theNumber == number) {
                return i;
            }
        }
        return -1;
    }

}

5、

package com.wanxi;

//需求:已知一个数组 arr = {19, 28, 37, 46, 50}; 用程序实现把数组中的元素值交换,
//交换后的数组 arr = {50, 46, 37, 28, 19}; 并在控制台输出交换后的数组元素。
public class ChangArray {
    public static void main(String[] args) {
        int[] array = {19, 28, 37, 46, 50};

        changeArray(array);

        runArray(array);
    }


    private static void runArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");

        }
    }

    private static void changeArray(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            int temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
    }
}

6、

package com.wanxi;

//需求:在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分。
//选手的最后得分为:去掉一个最高分和一个最低分后 的4个评委平均值 (不考虑小数部分)。
//测试用例1:[90,89,88,100,80,100], 输出:90+89+88+100 = 367. 367/4=91.75 ;
public class Grade {
    public static void main(String[] args) {
        int[] score = {90, 89, 88, 100, 80, 100};
        int sum = 0;
        int max = score[0];
        int min = score[0];
        int theAvg = getTheAvg(score, sum, max, min);
//        System.out.println(theAvg);
        Util.print("去掉一个最高分:{1},去掉一个最低分:{2},平均得分:{3}",max,min,theAvg);
    }






    private static int getTheAvg(int[] score, int sum, int max, int min) {
        for (int i = 0; i < score.length; i++) {
            sum += score[i];
            max = score[i] > max ? score[i] : max;
            min = score[i] < min ? score[i] : min;
        }
        int theAvg= (sum - max - min)/(score.length-2);
        return theAvg;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值