catalogue
Buy air tickets
Demand: the ticket price is charged according to the off-season and peak season, first-class and economy class. Enter the original ticket price, month and first-class or economy class.
Calculate the ticket price according to the following rules: 10% off for first class in peak season (may October), 8.5% off for economy class, 7% off for first class and 6.5% off for economy class in off-season (November to next April).
package com.beijing.test; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Enter ticket System.out.print("Please enter the original ticket price:"); double price = sc.nextDouble(); //Enter month System.out.print("Please enter the month:"); int month = sc.nextInt(); //Enter bin type System.out.print("Please enter the bin type (first class or economy class):"); String type = sc.next(); //Call function to calculate double new_price = calc(price, month, type); System.out.println("The discounted ticket price is:"+new_price); } /* Define a method to receive the original ticket price, month, first class or economy class * */ public static double calc(double price, int month, String type ){ //Judge the size of the month. If it is a peak season if(month >= 5 && month <= 10){ //Then judge whether it is economy class or first class. Because the judgment is a specific value, use switch switch (type){ case "economy class": price *= 0.85; break; case "First class": price *= 0.9; break; default: System.out.println("The position you entered is abnormal"); price = -1; } //If it's off-season } else if(month == 11 || month == 12 || month >= 1 & month <= 4){ switch (type) { case "economy class": price *= 0.65; break; case "First class": price *= 0.7; break; default: System.out.println("The position you entered is abnormal"); price = -1; } }else{ System.out.println("Your month has been entered incorrectly"); price = -1; } return price; } }
Summary:
- If you are judging a range, use if; If the judged data is several specific values, use switch
- Remember to add break in the case
- Understand how to write functions and how to call functions
Find prime
Prime numbers are called prime numbers if they cannot be divided by other positive integers except 1 and itself.
Requirements: judge the number of primes between 101-200 and output all primes
package com.beijing.test; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { for (int i = 101; i <= 200 ; i++) { boolean flag = true; for (int j = 2; j < i / 2 ; j++) { //If the remainder is 0, it can be an integer, that is, it is not a prime number if (i % j == 0){ flag = false; //Description is not prime break; } } if(flag){ //The description is prime System.out.print(i + ", "); } } } }
Summary:
- How to find prime numbers? We can use this number to divide from 2 to half of the number. If the remainder is not 0, it means it is a prime number
- Division in java will be rounded by default, such as 5 / 2 = 3
Verification code generation
Requirements: define the method to randomly generate a 5-digit verification code, each of which may be a number, uppercase letter and lowercase letter.
Thinking: it is required to generate a 5-digit random verification code, so each bit can be random. However, there are three random situations: uppercase letters, lowercase letters and numbers. We can apply two layers of randomness. One layer of randomness is the type of letters or numbers. Another layer randomly into specific data
package com.beijing.test; import java.util.Random; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { //3. Call function String code = createCode(5); System.out.println(code); } public static String createCode(int n){ //1. Define the string to record the random verification code String code = ""; Random r = new Random(); //2. Define the for loop and generate an n-bit verification code for (int i = 0; i < n; i++) { //Generate a random character: upper and lower case letters, lower case letters and numbers int type = r.nextInt(3); //0,1,2 switch (type){ case 0: //Uppercase characters (A - Z 65 + 26) char ch = (char)(r.nextInt(26) + 65); code += ch; break; case 1: //Lowercase characters (a - z 97 + 26) char ch1 = (char)(r.nextInt(26) + 97); code += ch1; break; case 2: //number code += r.nextInt(10); //0-9 break; } } return code; } }
Summary:
- Adding string type and int type plays the role of string splicing;
- Add the char type and int type, and use the ascii number corresponding to the char type to add
- Assigning a variable with a large data range to a variable with a small data range requires forced type conversion of the variable with a large data range
Judges score
Demand: in the singing competition, 6 judges give scores to the contestants, and the score range is an integer between [0-100]. The final score of the contestant is the average score of the four judges after removing the highest score and the lowest score. Please complete the above process and calculate the score of the contestant.
analysis:
- Enter scores by dynamically initializing the array
- Traverse the array to find the highest score, lowest score and total score
package com.beijing.test; import java.util.Random; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { //1. Define a dynamic initialization array to receive scores int[] scores = new int[6]; //2. Enter score Scanner sc = new Scanner(System.in); for (int i = 0; i < scores.length; i++) { System.out.print("Please enter page"+i+1+"Scoring of judges: "); int score = sc.nextInt(); //Store the scores in the corresponding position of the array scores[i] = score; } //3. Traverse the array to find the maximum value, minimum value and total score int max = scores[0]; int min = scores[0]; int sum = 0; for (int i = 0; i < scores.length; i++) { if (scores[i] > max){ max = scores[i]; } if (scores[i] < min){ min = scores[i]; } sum += scores[i]; } System.out.println("The highest score is:"+max); System.out.println("The minimum score is:"+min); //4. Average score double result = (sum - max - min)/ (scores.length -2); System.out.println("The final score is:" + result); } }
Digital encryption
The digital password of a system: for example, 1983 is transmitted by encryption. The rules are as follows: first get each digit, then add 5 to each digit, then calculate the remainder of 10, and finally reverse all digits to get a new string of numbers.
package com.beijing.test; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { //1. Define an array to store the data to be encrypted System.out.print("Please enter the number of numbers you want to encrypt:"); Scanner sc = new Scanner(System.in); int length = sc.nextInt(); int[] arr = new int[length]; //2. Enter the number to be encrypted and store it in the array for (int i = 0; i < arr.length; i++) { System.out.print("Please enter the number you want to encrypt"+(i + 1)+"Number:"); int number = sc.nextInt(); arr[i] = number; } //3. Print the data and have a look System.out.println("The data to be encrypted is:"); printArray(arr); //4. Encrypt the data content in the array for (int i = 0; i < arr.length; i++) { arr[i] = (arr[i] + 5) % 10; } //5. Invert the encrypted contents in the array for (int i = 0, j = arr.length - 1; i < j; i++, j--){ //Directly exchange the values of the two positions int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } //Print the last encrypted result and see System.out.println("The final encrypted data is:"); printArray(arr); } //Define how to print arrays public static void printArray(int[] arr){ System.out.print("["); if (arr != null && arr.length > 0){ for (int i = 0; i < arr.length; i++) { System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ","); } } System.out.println("]"); } }
Summary:
1. Know how to invert array elements. Define the positions of two variables at the beginning and end of the array. One variable moves forward and the other variable moves backward to exchange the values at the positions of both sides synchronously