| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
axjj2728
7年前发布

Java 绝对值函数及其妙用

   <p>文章摘要</p>    <p>1、绝对值函数,用来获取表达式的绝对值。</p>    <p>2、绝对值函数,实现降序+升序输出。</p>    <p><strong>一、绝对值函数使用说明</strong></p>    <p>绝对值函数是JDK中Math.java中的实现方法,其用来得到表达式的绝对值。</p>    <p>其实现非常简单,源码如下:</p>    <pre>  <code class="language-java">/**       * 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;      }</code></pre>    <p><strong>二、绝对值的特性及其运用。</strong></p>    <ul>     <li>1、正数的绝对值是其本身。</li>     <li>2、负数的绝对值是其相反数。</li>     <li>3、零的绝对值是其本身。<br> 绝对值:自减函数配合绝对值,先降序再升序。</li>    </ul>    <pre>  <code class="language-java">int number = 6;  System.out.println("原值输出:");  while(number>=-6){      number --;      System.out.print(number+" ");  }  System.out.println("\n绝对值输出:");  number = 6;  while(number>=-6){      number --;      System.out.print(Math.abs(number)+" ");  }</code></pre>    <p>输出结果:</p>    <pre>  <code class="language-java">原值输出:  5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7   绝对值输出:  5 4 3 2 1 0  1  2  3  4  5  6  7</code></pre>    <p><strong>三、案例</strong></p>    <p>1、背景:输出如下图案。</p>    <pre>  <code class="language-java">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</code></pre>    <p>2、分析:</p>    <ul>     <li>1、A为中心点</li>     <li>2、每一行,先降序,再升序</li>     <li>3、字母可以换算成整数,'A' = 65。那么,每行首个输出字母为 'A' +行数。</li>     <li>4、每行左右对称,每行输出字母数 = 行数*2 +1(字母A);</li>    </ul>    <p>3、实现</p>    <ul>     <li> <p>1、实现分析中的1~3步。以‘A’为中心点,先降序,再升序输出每行图案。</p> <pre>  <code class="language-java">//调用    print(5);      /**     * 先降序,再升序 实现     * @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)+" ");        }    }</code></pre> <p>输出如下:</p> <pre>  <code class="language-java">F E D C B A B C D E F</code></pre> </li>     <li>2、步骤4中,每行输出字母数 = 行数*2 +1(字母A),那么:<br> 每行应该显示的字母除外的部分,打印空格。逻辑控制如下: <pre>  <code class="language-java">for(int j=0;j<2*row+1;j++){    //逻辑输出字母。先降序、再升序逻辑输出的字母    int printChar = 'A' + Math.abs(row-j);    //如果 [逻辑控制字母] 大于 [规定输出字母],则:    if(printChar>firstChar){        //输出空格        System.out.print("    ");    }else{        //输出字母        System.out.print(((char)printChar)+"    ");    }  }</code></pre> </li>     <li>3、完整代码:</li>    </ul>    <pre>  <code class="language-java">//完整调用  printWithRow(7);    /**   * 先倒序 再正序  输出 英文大写字母   *    * @param row 行   */  private static void printWithRow(int row){      for(int i=0;i<row;i++){          //规定输出字母。每行第一个显示出来的字母          int firstChar = 'A' + i;          for(int j=0;j<2*row+1;j++){              //逻辑输出字母。先降序、再升序逻辑输出的字母              int printChar = 'A' + Math.abs(row-j);              //如果 [逻辑控制字母] 大于 [规定输出字母],则:              if(printChar>firstChar){                  //输出空格                  System.out.print("    ");              }else{                  //输出字母                  System.out.print(((char)printChar)+"    ");              }          }          //输出回车          System.out.println();      }  }</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/6cc0f22cb34c</p>    <p> </p>    
 本文由用户 axjj2728 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1483415453974.html
Java Java开发