Java 基础知识点(必知必会其一)

  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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值