Programming logic training for java learning

Programming logic training for java learning

Case 1: buying air tickets

analysis

Variables to be entered: season (peak season / off-season can be combined with subsequent months), class (economy class / first class, string type), original ticket price (should be floating point), month (integer)
Definition method: enter the original price, month, and class type of the air ticket
The cabin type is string type, which is inconvenient to use for statement or if statement, so I choose switch... case... Statement
In this demand, the method needs to return the value by visual inspection, and the return value is the discounted air ticket price.

Programming logic

  • 1. First judge whether the month corresponds to the off-season or peak season;
  • 2. Then judge the class, economy class or first class;
  • 3. Calculate the ticket price through switch/case statement;
  • 4. Final output fare.
package Programming thinking training;
import java.util.Scanner;
public class one {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter the ticket price:");
        double money = sc.nextDouble();
        System.out.print("Please enter the month of purchase:");
        int month = sc.nextInt();
        System.out.print("Please enter the class type:");
        String type = sc.next();
        double money_finall = value(money,month,type);
        System.out.print("Final price:"+money_finall);

    }
    public static double value(double money,int month,String type){
        // Judge whether a month is off-season or peak season
        if(month >= 5 && month <= 10){
            switch (type){
                case "First class":
                    money = money * 0.9;
                    break;
                case "economy class":
                    money = money * 0.85;
            }

        }
        else if((month >= 11 && month <= 12) || (month >=1 && month <=4)){
            switch (type){
                case "First class":
                    money = money*0.7;
                    break;
                case "economy class":
                    money = money*0.65;
                    break;
            }
        }else {
            System.out.println("There is an error in the month you entered");
            money = -1;
        }
        return money;
    }
}

result

summary

Case 2: finding prime numbers

analysis

Find all prime numbers between 101 – 200
Enter a number to determine whether the number is a prime number
It is easy to find the prime number between 101 and 200. It is quite traversal. The difficulty is how to output all elements?

code

package Programming thinking training;
import java.util.Scanner;
public class two {
    public static void main(String[] args) {
        System.out.print("Please enter a number:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        searchnums(num);
        search();
    }

    public static void searchnums(int num) {

        if (num > 1) {
            boolean flage = true;
            for (int k = 2; k < num / 2; k++) {
                if(num % k==0){
//                    System.out.println("the" + num + "is a prime number");
                    flage = false;
                    break;
                }
            }
            if(flage){
                System.out.println("number"+num+"It's a prime.");
            }else {
                System.out.println("number"+num+"It's a prime.");
            }
        }else {
            System.out.println("Error in input data");
        }
    }
    public static void search(){
        System.out.println("101--200 Between are prime numbers:");
        for(int i=100;i<201;i++){
            boolean f = true;
            for(int j = 2;j<i /2;j++){
                if(i % j == 0){
                    f = false;
                    break;
                }
            }
            if(f){
                System.out.print(i+";");
            }
        }
    }
}

result

summary

Case 3: development verification code

analysis

Randomly generate a 5-digit verification code: number, uppercase letter and lowercase letter. Note here that the number of verification codes is 5 and the generated categories are 3. However, java cannot generate letters, so forced character conversion is required. / / lowercase characters a:97 – z: 97+25// Capital A:65 – Z:65+25.
Define method: the element passed in by the method is num, that is, how many verification codes are generated, and the return value is the verification code.

/*
* Generate verification code.
* */

package Programming thinking training;
import java.util.Random;
public class three {
    public static void main(String[] args) {
        String str = code(5);
        System.out.println(str);
    }
    public static String code(int num){
        String str = "";
        Random r = new Random();
        for(int i = 0;i < num;i++){
            int type = r.nextInt(3); // 0: number, 1: uppercase, 2: lowercase
            switch (type){
                case 0:
                    int k = r.nextInt(10);
                    str += k;
                    break;
                case 1:
                    // Lowercase characters a:97 -- z: 97+25
                    char ch = (char) (r.nextInt(26)+97);
                    str += ch;
                    break;
                case 2:
                    // Capital A:65--Z:65+25
                    char CH = (char) (r.nextInt(26)+65);
                    str += CH;
                    break;
            }
        }
        return str;
    }
}

result

summary

Case 4: copying array elements

analysis

Define a method to traverse an array and assign its value to another array.
Method call: define a method to traverse the array and call the process of array assignment.

package Programming thinking training;

public class four {
    public static void main(String[] args) {
        int[] arr1 = {10,20,30,40,80,50};
        int[] arr2 = new int[arr1.length];
        copy(arr1,arr2);
    }
    public static void printarray(int[] arr){
        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]+";");
            }
        }
    }
    public static void copy(int[] arr,int[] arr2){

        for (int i = 0;i < arr.length;i++){
            arr2[i] = arr[i];
        }
        System.out.println("Array 1 Contents:");
        printarray(arr);
        System.out.println();
        System.out.println("Array 2 Contents:");
        printarray(arr2);

    }
}

result

Case 5: scoring by judges

analysis

1. The judges give a score. In real cases, it should be of double type.
2. Define a method to pass the judges' scores in real time and pass them into the array for storage.
3. Define a method to calculate the average score after removing the highest and lowest scores

code

package Programming thinking training;
import java.util.Scanner;
public class five {
    public static void main(String[] args) {
        double[] arr1 = new double[6];
        System.out.println("Please enter your score:");
        for(int i = 0;i< arr1.length;i++){
            Scanner sc = new Scanner(System.in);
            double num  = sc.nextDouble();
            arr1[i] = num;
        }
        System.out.println("The score you entered is:");
        System.out.print("[");
        for (int j = 0;j<arr1.length;j++){
            if(j == arr1.length -1){
                System.out.println(arr1[j]+"]");
            }else {
                System.out.print(arr1[j] + ";");
            }
        }
        score(arr1);
    }
    public static void score(double[] arr){
        double max = arr[0];
        double min = arr[0];
        double sum = 0.0;
        for(int i = 0;i< arr.length-1;i++){
            if(arr[i] > max){
                max = arr[i];
            }else if((arr[i] < min)){

                min = arr[i];
                }
            }
        for (int i =0;i < arr.length;i++){
            sum += arr[i];
        }
        double lastsum = (sum - max - min) / (arr.length - 2);
        System.out.println("The average score is:");
        System.out.println(lastsum);
        }
}

summary

Case 6: digital encryption

analysis

Define a method to traverse the array and output.
Operate the elements of the array according to the process. Pay attention to the difference between / and%

code

package Programming thinking training;
/*Digital encryption*/
public class six {
    public static void main(String[] args) {
        int [] arr = {4,7,8,5};
        System.out.println("Original array:");
        printarray(arr);

        for(int i = 0;i < arr.length;i++){
            arr[i] += 5;
        }
        System.out.println("+5 Post array:");
        printarray(arr);

        for(int j=0;j<arr.length;j++){
            arr[j] %= 10;
        }
        System.out.println("Array after 10 remainder:");
        printarray(arr);

        int j = arr.length - 1;
        for (int k = 0;k<j;k++,j--){
            int tem = 0;
            tem = arr[k];
            arr[k]= arr[j];
            arr[j] =tem;
        }
        System.out.println("Array after exchange:");
        printarray(arr);
    }
    public static void printarray(int[] arr){
        System.out.print("[");
        for(int i =0;i<arr.length;i++){
            if (i == arr.length-1){
                System.out.println(arr[i]+"]");
            }else {
                System.out.print(arr[i]+";");
            }
        }
    }

}

result

summary

Case 7: Digital decryption

The result of case 6 is decoded into the original array
The difficulty is% how to decode
Considering that the maximum number of each number will not exceed 9, I choose to add 10 in the decoding process, and then take% to get the single digit.

code

package Programming thinking training;
/*Digital decryption*/
public class seven {
    public static void main(String[] args) {
        int[] arr = {0,3,2,9};

        int j = arr.length-1;
        for (int k =0;k<j;k++,j--){
            int tem= 0;
            tem = arr[k];
            arr[k] = arr[j];
            arr[j] = tem;
        }

        for (int i = 0;i<arr.length;i++){
            arr[i] += 10;
            arr[i] -=5;
            if(arr[i] > 10){
                arr[i] %= 10;
            }
        }
        printarray(arr);

    }
    public static void printarray(int[] arr){
        System.out.print("[");
        for(int i =0;i<arr.length;i++){
            if (i == arr.length-1){
                System.out.println(arr[i]+"]");
            }else {
                System.out.print(arr[i]+";");
            }
        }
    }
}

result

reference resources

Dark horse programmer

Tags: Java

Posted on Sun, 28 Nov 2021 18:22:52 -0500 by exec1