栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021年10月11日练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021年10月11日练习

习题目录
  • 数组寻找最大值
  • 个位和十位都不是7,并且只能是偶数
  • 百钱白鸡
  • 两个数组的比较
  • 输入一个数,查找这个数在数组中的索引值
  • 翻转数组
  • 评委打分,计算最后分数
  • 兔子出生
  • 逢七必过
  • Other

数组寻找最大值
public class array_exercise {
    public static void main(String[] args) {
//        //数组寻找最大值
//        int[] arr = new int[]{12, 23, 43, 1, 23, 345};
//        int max = arr[0];
//        for (int i = 1; i < arr.length; i++) {
//            if (arr[i] > max) {
//                max = arr[i];
//            }
//        }
//        System.out.println(max);

//        //通过三目运算式输出最大值
//        int[] arr = {1, 2, 3, 54, 65, 324, 6, 4636, 234, 534, 6};
//        int max = arr[0];
//        for (int i=1;i
//            max = arr[i]>max ?  arr[i] : max ;
//            System.out.println(max);
//
//        }

    }
}
个位和十位都不是7,并且只能是偶数
public class ArraySummation {
    public static void main(String[] args) {
        int sum=0;
        int[] arr;
        int[] arr2 = new int[12];
        arr = new int[]{68, 27, 95, 88, 171, 996, 51, 210};
        for (int i = 0; i < arr.length; i++) {
            int a = arr[i] % 10;//个位是7
            int b = arr[i] / 10 % 10;//十位是7
//            System.out.println(arr[i]);
            if (a != 7 && b != 7 && arr[i] % 2 == 0) {
                System.out.println("符合要求的数字:"+arr[i]);
                arr2[i]=arr[i];
            }
        }
        for (int j=0;j 
百钱白鸡 
public class Buy_chicken_problem {
    public static void main(String[] args) {
        for (int x=1;x<100;x++){
            for (int y=1;y<100;y++){
                for (int z=1;z<100;z++){
                    if (x+y+z==100 && 5*x+3*y+z/3==100){
                        System.out.println("穷举法:"+x+"t"+y+"t"+z);
                    }
                }
            }
        }
    }
}
两个数组的比较

public class CompareArrays {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] arr2 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 78};
        int[] arr3 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println(getCompareArrays(arr1, arr2));
    }
    public static boolean getCompareArrays(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i]!=arr2[i]){
                return false;
            }
        }
        return true;
    }
}
输入一个数,查找这个数在数组中的索引值
import java.util.Scanner;

public class FindProblems {
    public static void main(String[] args) {
        int[] arr = new int[]{19, 28, 37, 46, 50};
        System.out.println("Please enter a number:");
        Scanner n = new Scanner(System.in);
        int N = n.nextInt();
        System.out.println(getIndex(arr,N));

    }
    //查找这个数是否为数组中的元素,如果不是返回-1,如果是的返回对应的索引
    public  static  int getIndex(int arr[],int N){

        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (N == arr[i]) {
                index=i;
                break;
            }
        }
        return index;
    }
}
翻转数组
public class InvertArray {
    public static void main(String[] args) {
        int[] arr;
        arr = new int[]{19, 28, 37, 46, 50};
        InverArray(arr);
    }

    public static void InverArray(int arr[]) {
        int[] arrtmp = new int[100];
        for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
//            System.out.println(arr[start]);
            arrtmp[start] = arr[start];
            arr[start] = arr[end];
            arr[end]=arrtmp[start];
//            int tmp = arr[start];
//            arr[start] = arr[end];
//            arr[end] = tmp;
        }
        System.out.print("[");
        for (int i = 0; i < arr.length; i++)
            if (i == arr.length-1) {
                System.out.print(arr[i]);

            }else {
                System.out.print(arr[i]+",");
            }
        System.out.print("]");
    }
}
评委打分,计算最后分数
import java.util.Scanner;


public class JudgesScore {
    public static void main(String[] args) {
        Scanner score = new Scanner(System.in);
        int[] arr = new int[6];
        int length=arr.length;
        for (int i = 0; i < length; i++) {
            System.out.println("Please rate the "+(i+1)+" first judge:");
            int a = score.nextInt();
            if (a > 0 && a < 100) {

                arr[i] = a;
            } else {
                System.out.println("Erro");
                length = arr.length+1;
            }
        }
        System.out.println(getScore(arr));
    }

    // 根据要求去掉最大值和最小值求得平均分
    public static int getScore(int arr[]) {
        int max = arr[0], min = arr[0], score = 0, sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (max > arr[i]) {
                max = arr[i];
                return max;
            }
            if (min < arr[i]) {
                min = arr[i];
                return min;
            }
            sum += arr[i];
            return sum;
        }
        score = (sum - max - min) / arr.length - 2;
        return score;

    }

}
兔子出生
public class rabbit_practice {
    public static void main(String[] args) {
        //关于兔子出生的案例
        int[] rb = new int[20];
        rb[0] = 1;
        rb[1] = 1;
        for (int i=2;i 
逢七必过 
public class Skip_seven {
    public static void main(String[] args) {
        getSeven();//缝七必过
    }
    //遇见7 或者是含有7或者是7的倍数则跳过
    public static void getSeven() {
        int a, b, c;
        for (int i = 1; i < 100; i++) {
            a = i % 10;
            b = i / 10 % 10;
            c = i % 7;
            if (a == 7 || b == 7 || c == 0) {
                System.out.println(i);
            }
        }
    }
}
Other
public class method_study {
    public static void main(String[] args) {

    }
    //数组最大值
    public static int getarraryN(int[] ar) {
        int max=ar[0];
        for (int i=0;i max) {
                max = ar[i];
            }
        }
        System.out.println(max);
        return max;
    }

    // 数组遍历
    public static int getArrary() {
        int[] arry = new int[]{11, 22, 33, 44, 55};
        System.out.print("[");
        for (int i = 0; i < arry.length; i++) {
            if (i == arry.length - 1) {
                System.out.print(arry[i]);
            } else {
                System.out.print(arry[i] + ",");
            }
        }
        System.out.println("]");

        return 0;
    }

    //方法的重载代码
    public static boolean getint(int a, int b) {
        System.out.println("int");
        return a == b;
    }

    public static boolean getint(long a, long b) {
        System.out.println("long");
        return a == b;
    }

    public static boolean getint(short a, short b) {
        System.out.println("short");
        return a == b;
    }

    public static boolean getint(byte a, byte b) {
        System.out.println("byte");
        return a == b;
    }

    //给定方法判段ab哪一个更大
    public static int getmax(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    //给定参数判断是否为偶数
    public static boolean getEvenNumber(int a) {
        if (a % 2 == 0) {
            return true;
        } else {
            return false;
        }

    }

    //获取两个数的最大值(有参)
    public static void getMax(int a, int b) {
        if (a > b) {
            System.out.println(a);
        } else {
            System.out.println(b);
        }

    }

    //获取两个数的最大值(无参)
    public static void LargerNumber() {
        Scanner n = new Scanner(System.in);
        int a = n.nextInt();
        int b = n.nextInt();
        if (a > b) {
            System.out.println(a);
        } else {
            System.out.println(b);
        }
    }

    //水仙花数
    public static void NarcissisticNumber() {
        for (int i = 100; i < 1000; i++) {
            int b = i / 100;
            int s = i / 10 % 10;
            int g = i % 10;
            if (b * b * b + s * s * s + g * g * g == i) {
                System.out.println(i);
            }
        }
//        int count=0;
//            for(int j=1;j<10;j++){
//                for (int n=1;n<10;n++){
//                    for (int b=1;b<10;b++){
//                        if(n==j+1&&b==n+1){count++;System.out.println(""+j+n+b+n+j);}
//
//                    }
//                }
//            }
//        System.out.println(count);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号