The introduction of absolute value function in Java and its wonderful use

  • 2020-05-27 05:38:16
  • OfStack

1. Instructions for absolute value functions

The absolute value function is JDK Math.java Is used to obtain the absolute value of an expression.

Its 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 negation 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 value and its application.

1. The absolute value of a positive number is itself.

The absolute value of a negative number is its negative number.

3. The absolute value of zero is itself.

Absolute value: self - subtracting function with absolute value, first in descending order and then in ascending order.


int number = 6;
System.out.println(" Output of original value: ");
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 results:


 Output of original value: 
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

Case 3.

1. Background: output the following pattern.


  A 
  B A B 
  C B A B C 
 D C B A B C D 
 E D C B A B C D E 
 F E D C B A B C D E F 
 G F E D C B A B C D E F G

2. Analysis:

1. A is the central point

2. For each row, the descending order is followed by the ascending order

3. The letter can be converted to an integer, 'A' = 65. So, the first output letter of each line is 'A' + the number of lines.

4. The left and right sides of each line are symmetrical, and the number of letters output in each line = the number of lines *2 +1 (letter A);

3, implementation,

1. Implement steps 1 to 3 in the analysis. With 'A' as the center, output each line in descending order and then in ascending order.


 // call 
 print(5);

 /**
 *  Descending, then ascending   implementation 
 * @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:


F E D C B A B C D E F

2. In step 4, the number of letters output per line = the number of lines *2 +1 (letter A), then:

Each line should display the letters except for the printed Spaces. The logic control is as follows:


for(int j=0;j<2*row+1;j++){
 // Logic outputs letters. A letter that is output in descending order and then in ascending order 
 int printChar = 'A' + Math.abs(row-j);
 // if  [ Logic control letter ]  Is greater than  [ Specified output letter ] , then: 
 if(printChar>firstChar){
 // The output space 
 System.out.print(" ");
 }else{
 // The output of letters 
 System.out.print(((char)printChar)+" ");
 }
}

3. Complete code:


// Complete call 
printWithRow(7);

/**
 *  The first order   Positive sequence again   The output   Capital letter 
 * 
 * @param row  line 
 */
private static void printWithRow(int row){
 for(int i=0;i<row;i++){
 // Specifies the output letter. Each row of the first 1 The letters are displayed 
 int firstChar = 'A' + i;
 for(int j=0;j<2*row+1;j++){
  // Logic outputs letters. A letter that is output in descending order and then in ascending order 
  int printChar = 'A' + Math.abs(row-j);
  // if  [ Logic control letter ]  Is greater than  [ Specified output letter ] , then: 
  if(printChar>firstChar){
  // The output space 
  System.out.print(" ");
  }else{
  // The output of letters 
  System.out.print(((char)printChar)+" ");
  }
 }
 // Output the enter 
 System.out.println();
 }
}

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: