[12] Java basics exercise

Posted by marcs910 on Thu, 13 Jan 2022 15:25:11 +0100

Weight loss plan (if)

Demand: enter the number of weeks and display the corresponding activities
Monday: running
Tuesday: swimming
Wednesday: jogging
Thursday: sleep
Friday: walk
Saturday: cycling
Sunday: Travel

Code demonstration

public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a weight loss plan for the day of the week:");
        int Week = sc.nextInt();
        if (Week>7||Week<1){
            System.out.println("Please enter 1-7 Number of");
        }
        else if (Week==1){
            System.out.println("run");
        }
        else if (Week==2){
            System.out.println("Swimming");
        }
        else if (Week==3){
            System.out.println("jogging");
        }
        else if (Week==4){
            System.out.println("sleep");
        }
        else if (Week==5){
            System.out.println("take a walk");
        }
        else if (Week==6){
            System.out.println("ride a bike");
        }
        else{
            System.out.println("Travel");
        }
    }

Weight loss plan (switch)

Demand: enter the number of weeks and display the corresponding activities
Monday: running
Tuesday: swimming
Wednesday: jogging
Thursday: sleep
Friday: walk
Saturday: cycling
Sunday: Travel

Code demonstration

 public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a weight loss plan for the day of the week:");
        int Week = sc.nextInt();
        switch (Week) {
            case 1:
                System.out.println("run");
                break;
            case 2:
                System.out.println("Swimming");
                break;
            case 3:
                System.out.println("jogging");
                break;
            case 4:
                System.out.println("sleep");
                break;
            case 5:
                System.out.println("take a walk");
                break;
            case 6:
                System.out.println("ride a bike");
                break;
            case 7:
                System.out.println("Travel");
                break;
            default:
                System.out.println("Input error");
        }
    }

Every seven

Demand: start counting from any number. When the number reported contains 7 or a multiple of 7, it should be said that in order to make everyone familiar with the game, print out the number of 1-100 from the printing desk to meet the number of every seven

Code demonstration

public static void main(String[] args){
        for (int x = 1;x<=100;x++){
            if (x%7==0||x%10==7||x/10%10==7){
                System.out.print(x+" ");
            }
        }
    }

Immortal rabbit

Demand: there is a pair of rabbits. From the third month after birth, a pair of rabbits are born every month. After the third month, a pair of rabbits are born every month. If the rabbits do not die, what are the logarithm of rabbits in the twentieth month?

Code demonstration

public static void main(String[] args){
        int[] arr = new int[20];
        arr[0] = 1;
        arr[1] = 1;
        for (int x=2;x< arr.length;x++){
            arr[x] = arr[x-2]+arr[x-1];
        }
        System.out.println("The number of rabbits in the 20th month is:"+arr[19]);
    }

A hundred dollars and a hundred chickens

Demand: the mathematical problem put forward by the ancient mathematician Zhang Qiujian in the book Suanjing: the chicken Weng is worth five, the chicken mother is worth three, and the chicken chick is worth three. If you buy a hundred chickens for a hundred dollars, ask the chicken owner, the chicken mother and the chicken chick?

Code demonstration

public static void main(String[] args){
        for (int x = 0;x<=20;x++){
            for (int y = 0;y<=33;y++){
                int z=100-x-y;
                    if(z%3==0&&5*x+3*y+z/3 ==100){
                        System.out.println("cock:"+x+"   hen"+y+"    chick"+z);
                    }
            }
        }
    }

Summation of array elements

Requirement: there is an array with elements {68, 27, 95, 88171996, 51210} Find the sum of elements that meet the requirements in the array. The requirements are: the single bit and ten bit of the summation element cannot be 7, and can only be an even number

Code demonstration

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++){
            int x =arr[i];
            if (x%2==0&&x%10!=7&&x/10%10!=7){
                sum=sum+x;
            }
        }
        System.out.println("Elements and are:"+sum);
    }

The contents of the array are the same

Requirement: design a method to compare whether two arrays are the same

Code demonstration

public static void main(String[] args){
        int[] arr = {11,22,44,44,55};
        int[] arr2 = {11,22,33,44,55};
        boolean m = compare(arr,arr2);
        System.out.println(m);
    }
    public static boolean compare(int[] arr,int[] arr2){
        if(arr.length!=arr2.length){
            return false;
        }
        for (int x =0;x<arr.length;x++){
            if (arr[x]!=arr2[x]){
                return false;
            }
        }
        return true;
    }

lookup

An array arr={19, 28, 37, 46, 50} is known; Enter a data on the keyboard, find the index of the data in the array, and output the index value on the console

Code demonstration

public static void main(String[] args){
        int[] arr = {19,28,37,46,50};
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter:");
        int x= sc.nextInt();
        int index= getChar(arr,x);
        System.out.println("Index value="+index);
    }
    public static int getChar(int[] arr,int x){
        int index = -1;
        for (int i = 0;i< arr.length;i++){
            if (x==arr[i]){
                index = i;
                break;
            }
        }
        return index;
    }

reversal

Requirement: an array arr = {19,28,37,46,50} is known; Exchange the elements in the array with a program
Array after exchange arr = {50,46,37,25,19}; And output the exchanged array on the console

Code demonstration

public static void main(String[] args){
        int[] arr = {19,28,37,46,50};
        for (int start = 0,end = arr.length-1;start<=end;start++,end--){
            int temp = arr[start];
            arr[start]=arr[end];
            arr[end]=temp;
        }
        printArray(arr);
    }
    public static void printArray(int[] arr){
        System.out.print("[");
        for (int x = 0;x< arr.length;x++){
            if (x==  arr.length-1){
                System.out.print(arr[x]);
            }else{
                System.out.print(arr[x]+",");
            }
        }
        System.out.print("]");
    }

Judges score

Requirements: Six judges will score, the score is an integer of 0-100, and the final score of the contestant is the average of the four scores after removing the highest score and the lowest score

Code demonstration

public static void main(String[] args){
        int[] arr=new int[6];
        Scanner sc = new Scanner(System.in);
        for (int i = 0;i< arr.length;i++){
            System.out.println("Please enter page"+(i+1)+"Scores of judges");
            arr[i] = sc.nextInt();
        }
        int max = getMax(arr);
        int min = getMin(arr);
        int sum = sum(arr);
        int avg = (sum-max-min)/(arr.length -2) ;
        System.out.println("The final score of the contestant is:"+avg);
    }
    public static int getMax(int[] arr){
        int max = arr[0];
        for (int x = 0;x< arr.length;x++){
            if (arr[x]>max){
                max = arr[x];
            }
        }
        return max;
    }
    public static int getMin(int[] arr){
        int min = arr[0];
        for (int x = 0;x< arr.length;x++){
            if (arr[x]<min){
                min = arr[x];
            }
        }
        return min;
    }
    public static int sum(int[] arr){
        int sum = 0;
        for (int i = 0;i< arr.length;i++){
            sum = sum+arr[i];
        }
        return sum;
    }

Topics: Java Back-end