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

时间:2023-03-08 16:05:24
Java 基础知识点(必知必会其一)
  1. 如何将字符串转换为数字?
  2.  package Day_2;
    /**
    * @author Administrator
    * 功能: 如何将字符串转换为数字?
    */
    public class StringToInteger {
    public static void main(String args [])
    {
    String ss="123";
    int val = Integer.parseInt(ss);
    System.out.println(val);
    ss="123.4";
    float va =Float.parseFloat(ss);
    System.out.println(va);
    }
    }

    2.如何将数字转换为十六进制字符串?

  3.  public class HexToOxc {
    
         private static final  String dic = "0123456789ABCDEF";;
    
         /**
    * @param args
    * 如何将数字转换为十六进制字符串?
    */ public static void main(String[] args) {
    // TODO Auto-generated method stub int num=0;
    String ans="";
    Scanner reader = new Scanner(System.in);
    while(reader.hasNext()){
    num = reader.nextInt();
    ans=IntToOxc(num);
    System.out.println(ans);
    } } static String IntToOxc(int num){
    String ss="";
    while(num>0){
    int pos = num%16 ;
    ss+=dic.charAt(pos);
    num/=16;
    }
    return (new StringBuffer(ss)).reverse().toString();
    }
    }
  4. 如何将字节串转换为十六进制字符串?
    1.  package com.Gxjun.problem;
      
       import java.util.Scanner;
      
       public class Answer {
      //如何将字节串转换为十六进制字符串?
      public static void main(String args []){
      String a1 = "";
      Scanner reader = new Scanner(System.in);
      TenToSixTeen tst = new TenToSixTeen();
      while(reader.hasNext()){
      a1= reader.next();
      tst.StringToIntgeer(a1);
      System.out.println(tst.DealToDec());
      }
      }
      }
      //定义一个类,处理这个字符串
      class TenToSixTeen {
      /*
      * 我们可以这样来考虑,首先我们得到的字符串一定都是数字组成,
      * 字符串先转化为数字,然后在转化为十六进制数值
      */
      int num; /* 字符串转化为数值
      */
      public void StringToIntgeer(String str){
      num=0;
      int index=1;
      for(int i=str.length()-1;i>=0;i--){
      num+=(str.charAt(i)-'0')*index;
      index*=10;
      }
      } /* 数值转化为十六进制数值
      */
      public String DealToDec(){
      int sst=0;
      String st="";
      do{
      sst = num%16;
      num /= 16;
      if(sst > 9){
      sst-=10;
      st+=(char)(sst+97);
      }
      else {
      st+=(char)(sst+48);
      }
      }while(num>0); return ((new StringBuffer(st)).reverse()).toString();
      }
    2. 如何对浮点数打印出指定小数位数?

      1.  package com.Gxjun.problem;
        
           /*
        * 如何对浮点数打印出指定小数位数?
        */ import java.math.BigDecimal; public class FreFloat {
        public static void main(String args []){
        System.out.println(divdouble(1.123456,1,5));
        System.out.println(divdouble1(1.123456,1,5));
        } //四舍五入模式
        public static double divdouble(double d1,double d2,int len)
        { BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.divide(b2,len, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        //非四舍五入模式
        public static double divdouble1(double d1,double d2,int len)
        { BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.divide(b2,len,1).doubleValue();
        }
        }

        2.如何将浮点数输出为指定位数的科学计数法?

      2.  package com.Gxjun.problem;
        
         import java.math.BigDecimal;
        import java.text.DecimalFormat;
        import java.util.Scanner; /*
        *如何将浮点数输出为指定位数的科学计数法?
        * 输入下列的精确度和科学计数的长度
        */ public class SenciceFloat { public static void main( String args[] ){
        double aa= 123.12345;
        int a,b;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()){ a = reader.nextInt();
        b = reader.nextInt();
        System.out.println(Function(aa,a,b));
        }
        }
        public static String Function(double num,int aa ,int bb ){
        DecimalFormat Decf = new DecimalFormat();
        String at="",bt="";
        for(int i=0;i<aa;i++) at += "0";
        for(int i=0;i<bb;i++) bt += "0";
        Decf.applyPattern("0."+at+"E"+bt);
        return Decf.format(num);
        }