1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問題

        時(shí)間:2023-03-18 17:02:32 JAVA認(rèn)證 我要投稿
        • 相關(guān)推薦

        Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問題

          java 字符串轉(zhuǎn)化整型問題

        Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問題

          public class StringParesInteger {

          /**

          * @param args

          */

          public static void main(String[] args) {

          // TODO Auto-generated method stub

          System.out.println(Integer.MIN_VALUE);

          System.out.println(Integer.MAX_VALUE/10);

          System.out.println(pareseInt("="));

          }

          /**本題考查的主要是邊界條件

          * 1.穿入的字符串是否為空

          * 2.字符串的首位是否為(+、-)

          * 3.字符中是否有非法字符

          * 4.穿入的字符串是否超過了整數(shù)的最大值(Integer.MAX_VALUE(2147483647)/Integer.MIN_VALUE(-2147483648))

          *

          * @param data

          * @return

          */

          public static int pareseInt(String data){

          /*

          * 判讀穿傳入的字符串是否為空

          */

          if(data==null||data.length()==0){

          throw new NullPointerException("data is null");

          }

          int index=0;

          /**

          *

          */

          //是否為負(fù)數(shù)

          boolean isPositive=true;

          // 臨界值

          int limit = 0;

          //取出字符串的第一位

          char first=data.charAt(0);

          //第一位是負(fù)數(shù)的情況下

          if(first=='-'){

          isPositive=false;

          index++;

          //設(shè)置整形最小的負(fù)數(shù)(-2147483648)

          limit=-Integer.MIN_VALUE;

          }

          //第一位是整數(shù)的情況下

          if(first=='+'){

          isPositive=true;

          //設(shè)置最大的正數(shù)是(2147483647)

          limit=Integer.MAX_VALUE;

          index++;

          }

          //設(shè)置比較的邊界值(214748364)

          int maxLimit=Integer.MAX_VALUE/10;

          int length=data.length();

          int result=0;

          while(index'0'&&ch<'9'){

          //先判斷原來的值是否大于比較的臨界值

          if(result>maxLimit){

          throw new RuntimeException("整數(shù)越界了");

          }

          // 判斷當(dāng)前位的值+ch的值是否》整數(shù)的最大值

          if(result*10>limit-(ch-'0')){

          System.out.println("result-->"+(result*10));

          System.out.println("max----->"+(Integer.MAX_VALUE));

          System.out.println("result-->"+(Integer.MAX_VALUE-(ch-'0'))+" ch="+(ch-'0'));

          throw new RuntimeException("數(shù)組越界了s ");

          }

          index++;

          result=result*10+(ch-'0');

          }else{

          throw new RuntimeException("不是整數(shù) ");

          }

          }

          //三目運(yùn)算符

          return isPositive?result:-result;

          }

          }

        【Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問題】相關(guān)文章:

        sun認(rèn)證java關(guān)于字符串處理技巧03-29

        JAVA認(rèn)證基礎(chǔ)知識(shí):JavaNativeInterface學(xué)習(xí)小結(jié)01-11

        SUN JAVA認(rèn)證介紹12-18

        Java的基礎(chǔ)知識(shí)07-27

        關(guān)于JAVA字符串的拼接與性能06-06

        sun java認(rèn)證報(bào)考指南03-08

        java認(rèn)證考試培訓(xùn)內(nèi)容03-27

        JAVA認(rèn)證基礎(chǔ)知識(shí):基于反射機(jī)制的服務(wù)代理調(diào)用03-08

        sun認(rèn)證java程序員須知Java日志框架03-30

        Java基礎(chǔ)知識(shí)詳解12-07

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>