1. Description of the usage of absolute value function
Absolute value function is an implementation method in Math.java in JDK, which is used to obtain the absolute value of an expression.
The implementation is very simple, the source code is as follows:
/** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negotiation of the argument is returned. * * <p>Note that if the argument is equal to the value of * {@link Integer#MIN_VALUE}, the most negative representable * {@code int} value, the result is that same value, which is * negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static int abs(int a) { return (a < 0) ? -a : a; }2. Characteristics of absolute values and their application.
1. The absolute value of a positive number is itself.
2. The absolute value of a negative number is its opposite number.
3. The absolute value of zero is itself.
Absolute value: The self-decreasing function is combined with the absolute value, first descending order and then ascending order.
int number = 6;System.out.println("original value output:"); while(number>=-6){ number --; System.out.print(number+" ");}System.out.println("/n absolute value output:");number = 6;while(number>=-6){ number --; System.out.print(Math.abs(number)+" ");}Output result:
Original value output: 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 Absolute value output: 5 4 3 2 1 0 1 2 3 4 5 6 7
III. Case
1. Background: The following pattern is output.
ABABCBABCDCBABCDEDCBABCDEDCBA BCDEFEDCBABCDEFGFEDCB ABCDEFG
2. Analysis:
1. A is the center point
2. Each line, descending first, then ascending
3. Letters can be converted into integers, 'A' = 65. Then, the first output letter for each line is 'A' + number of lines.
4. Each line is symmetrical left and right, and the number of letters output per line = number of lines *2 +1 (letter A);
3. Realize
1. Realize 1 to 3 steps in the analysis. With 'A' as the center point, first descending order, and then output each line of pattern in ascending order.
//Call print(5); /** * Implement in descending order first, then ascending order * @param row */ private static void print(int row){ for(int i=0;i<2*row+1;i++){ int printChar = 'A' + Math.abs(row-i); System.out.print(((char)printChar)+" "); } }The output is as follows:
FEDCBABCDEF
2. In step 4, the number of letters per line = number of lines *2 +1 (letter A), then:
Each line should display the parts except the letters, print spaces. The logic control is as follows:
for(int j=0;j<2*row+1;j++){ //Logistic output letters. The letters int printChar in descending order and then ascending order of logical output = 'A' + Math.abs(row-j); //If [logical control letter] is greater than [specified output letter], then: if(printChar>firstChar){ //Output space System.out.print(" "); }else{ //Output letter System.out.print(((char)printChar)+" "); }} 3. Complete code:
//Call printWithRow(7);/** * First in reverse order and then output English capital letters in positive order * * @param row line */private static void printWithRow(int row){ for(int i=0;i<row;i++){ //Specify output letters. The first letter displayed on each line is int firstChar = 'A' + i; for(int j=0;j<2*row+1;j++){ //Logistic output letters. The letters int printChar = 'A' + Math.abs(row-j); //If [logical control letters] are greater than [standard output letters], then: if(printChar>firstChar){ //Output space System.out.print(" "); }else{ //Output letters System.out.print(((char)printChar)+" "); } } //Output carriage return System.out.println(); }}Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.