Java 基礎知識點(必知必會其一)

weixin_33868027發表於2015-03-04
  1. 如何將字串轉換為數字?
  2.  1 package Day_2;
     2 /**
     3  * @author Administrator
     4  * 功能: 如何將字串轉換為數字?
     5  */
     6 public class StringToInteger {
     7     public static void main(String args [])
     8     {
     9         String ss="123";
    10         int val = Integer.parseInt(ss);
    11         System.out.println(val);
    12         ss="123.4";
    13         float va =Float.parseFloat(ss);
    14         System.out.println(va);
    15     }
    16 }

    2.如何將數字轉換為十六進位制字串?

  3.  1 public class HexToOxc {
     2 
     3     private static final  String dic = "0123456789ABCDEF";;
     4 
     5     /**
     6      * @param args
     7      *  如何將數字轉換為十六進位制字串?
     8      */
     9     
    10     public static void main(String[] args) {
    11         // TODO Auto-generated method stub
    12         
    13         int num=0;
    14         String ans="";
    15         Scanner  reader = new Scanner(System.in);
    16         while(reader.hasNext()){
    17             num = reader.nextInt();
    18             ans=IntToOxc(num);
    19             System.out.println(ans);
    20         }
    21         
    22     }
    23     
    24     static String IntToOxc(int num){
    25         String ss="";
    26         while(num>0){
    27          int pos = num%16 ;
    28           ss+=dic.charAt(pos);
    29           num/=16;
    30         }
    31         return (new StringBuffer(ss)).reverse().toString();
    32     }    
    33 }
  4. 如何將位元組串轉換為十六進位制字串?
    1.  1 package com.Gxjun.problem;
       2 
       3 import java.util.Scanner;
       4 
       5 public class Answer {
       6      //如何將位元組串轉換為十六進位制字串?    
       7   public static void main(String args []){
       8        String a1 = "";
       9        Scanner reader = new Scanner(System.in);
      10        TenToSixTeen  tst = new TenToSixTeen();
      11        while(reader.hasNext()){
      12         a1= reader.next();
      13         tst.StringToIntgeer(a1);
      14         System.out.println(tst.DealToDec());
      15     }
      16   } 
      17 }
      18    //定義一個類,處理這個字串
      19 class  TenToSixTeen    {
      20    /*
      21     * 我們可以這樣來考慮,首先我們得到的字串一定都是數字組成,
      22     * 字串先轉化為數字,然後在轉化為十六進位制數值
      23     */
      24     int num;
      25     
      26     /* 字串轉化為數值
      27      */
      28    public void StringToIntgeer(String str){
      29         num=0; 
      30         int index=1;
      31        for(int i=str.length()-1;i>=0;i--){
      32              num+=(str.charAt(i)-'0')*index;
      33              index*=10;
      34        }
      35    }
      36    
      37    /* 數值轉化為十六進位制數值
      38     */
      39  public String DealToDec(){
      40      int sst=0;
      41      String st="";
      42      do{
      43       sst = num%16;
      44       num /= 16;
      45       if(sst > 9){  
      46        sst-=10;
      47        st+=(char)(sst+97); 
      48       }
      49       else {
      50         st+=(char)(sst+48);
      51        }
      52      }while(num>0);
      53        
      54     return ((new StringBuffer(st)).reverse()).toString();
      55    }
      如何對浮點數列印出指定小數位數?
      1.  1 package com.Gxjun.problem;
         2 
         3   /*
         4    * 如何對浮點數列印出指定小數位數?
         5    */
         6 
         7 import java.math.BigDecimal;
         8 
         9 public class FreFloat {
        10    public static void main(String args []){
        11        System.out.println(divdouble(1.123456,1,5));  
        12        System.out.println(divdouble1(1.123456,1,5));  
        13    }
        14 
        15    //四捨五入模式
        16    public static double divdouble(double d1,double d2,int len)
        17    {
        18 
        19            BigDecimal b1 = new BigDecimal(Double.toString(d1)); 
        20            BigDecimal b2 = new BigDecimal(Double.toString(d2)); 
        21            return b1.divide(b2,len, BigDecimal.ROUND_HALF_UP).doubleValue();
        22    }
        23    //非四捨五入模式
        24    public static double divdouble1(double d1,double d2,int len)
        25    {
        26 
        27            BigDecimal b1 = new BigDecimal(Double.toString(d1)); 
        28            BigDecimal b2 = new BigDecimal(Double.toString(d2)); 
        29            return b1.divide(b2,len,1).doubleValue();
        30    }
        31 }

        2.如何將浮點數輸出為指定位數的科學計數法?

      2.  1 package com.Gxjun.problem;
         2 
         3 import java.math.BigDecimal;
         4 import java.text.DecimalFormat;
         5 import java.util.Scanner;
         6 
         7 /*
         8  *如何將浮點數輸出為指定位數的科學計數法?
         9  * 輸入下列的精確度和科學計數的長度
        10  */
        11 
        12 public class SenciceFloat {
        13     
        14   public static void main( String args[] ){
        15       double  aa= 123.12345; 
        16       int a,b;
        17       Scanner  reader = new Scanner(System.in);
        18       while(reader.hasNext()){
        19         
        20          a = reader.nextInt();
        21          b = reader.nextInt();
        22         System.out.println(Function(aa,a,b));
        23       }
        24     }
        25   public static String Function(double num,int aa ,int bb ){
        26       DecimalFormat  Decf = new DecimalFormat(); 
        27       String at="",bt="";
        28       for(int i=0;i<aa;i++)   at += "0";
        29       for(int i=0;i<bb;i++)   bt += "0";
        30       Decf.applyPattern("0."+at+"E"+bt);
        31       return Decf.format(num);
        32    }
        33

相關文章