久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

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

時間:2024-07-21 17:01:36 JAVA認證 我要投稿
  • 相關(guān)推薦

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

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

Java認證基礎(chǔ)知識: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;

  /**

  *

  */

  //是否為負數(shù)

  boolean isPositive=true;

  // 臨界值

  int limit = 0;

  //取出字符串的第一位

  char first=data.charAt(0);

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

  if(first=='-'){

  isPositive=false;

  index++;

  //設(shè)置整形最小的負數(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ù)越界了");

  }

  // 判斷當前位的值+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ù) ");

  }

  }

  //三目運算符

  return isPositive?result:-result;

  }

  }

《美女视频免费永久观看的网站,刘亦菲天梭视频.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

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

JAVA認證基礎(chǔ)知識:Java獲取當前的系統(tǒng)時間03-18

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

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

JAVA認證簡介03-19

SUN JAVA認證介紹12-18

JAVA認證考試細則03-19

java認證考試介紹03-19

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

JAVA認證開源技術(shù):關(guān)于Java的對象equals方法03-04

在线咨询

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

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

Java認證基礎(chǔ)知識: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;

  /**

  *

  */

  //是否為負數(shù)

  boolean isPositive=true;

  // 臨界值

  int limit = 0;

  //取出字符串的第一位

  char first=data.charAt(0);

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

  if(first=='-'){

  isPositive=false;

  index++;

  //設(shè)置整形最小的負數(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ù)越界了");

  }

  // 判斷當前位的值+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ù) ");

  }

  }

  //三目運算符

  return isPositive?result:-result;

  }

  }