The first chapter after-school exercise 1.3

1.3 printDigit only method for processing I / O's, a method to prepare any type double output amount (which may be negative).

Package com.algorithm.chapterone; 

/ ** 
 * only printDigit method for processing I / O's, a method to prepare any type double output amount (may be negative) 
 * @author Gao · Rongzheng 
 * 
 * / 
public  class QuestionThree { 

    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        Double NUM = -12.63 ;
         IF (NUM <0 ) { 
            of System.out.print ( "-" ); 
            NUM = NUM the Math.abs ( ); 
        } 
        int Integer = ( int ) NUM;
         int integerLength = Integer.toString(integer).length();
        int decimalLength = Double.toString(num).length() - integerLength - 1;
        
        printDigit(num, -integerLength, decimalLength);
    }
    
    public static void printDigit(double num, int integerLength, int decimalLength) {
        //※ 递归方法的的基准
        if (integerLength == decimalLength) { return; }
        //※ 往基准靠拢
        integerLength++;
        int n = (int) (num * Math.pow(10, integerLength));
        System.out.print(n % 10);
        //※ 判断输出小数
        if (integerLength == 0) {
            System.out.print(".");
        }
        printDigit(num, integerLength, decimalLength);
    }
}

 

Guess you like

Origin www.cnblogs.com/code-future/p/11419053.html