日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从键盘获取字符串,并把字符串转数字

發布時間:2024/10/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从键盘获取字符串,并把字符串转数字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.有很多漏洞的實現方法

public static void main(String[] args) {
?? ??? ?System.out.println("開始輸入:");
?? ??? ?//從鍵盤輸入字符串
?? ??? ?Scanner input = new Scanner(System.in);
?? ??? ?//獲取鍵盤輸入的字符串
?? ??? ?String str = input.nextLine();
?? ??? ?System.out.println("結束");
?? ??? ?//字符串轉字符數組
?? ??? ?char[] num = str.toCharArray();
?? ??? ?//使用StringBuffer對象存儲拼接后的數字
?? ??? ?StringBuffer hire = new StringBuffer();
?? ??? ?//如果字符串以“-”開頭
?? ??? ?if(str.startsWith("-")) {
?? ??? ??? ?//在拼接前給字符串加上符號
?? ??? ??? ?hire.append("-");
?? ??? ??? ?for (int i = 0; i < num.length; i++) {
?? ??? ??? ??? ?hire.append(num[i]);
?? ??? ??? ?}
?? ??? ?}else {
?? ??? ??? ?//Character.isDigit(a) 判斷a是否是數字,如果是,則返回true,否則返回false
?? ??? ??? ?//判斷字符串的開否是否是數字,如果不是,直接輸出0
?? ??? ??? ?if(!Character.isDigit(num[0])) {
?? ??? ??? ??? ?System.out.println(0);
?? ??? ??? ?}else {
?? ??? ??? ??? ?//如果是數字,則對數字進行拼接
?? ??? ??? ??? ?hire.append("-");
?? ??? ??? ??? ?for (int i = 0; i < num.length; i++) {
?? ??? ??? ??? ??? ?hire.append(num[i]);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println(hire);
?? ?}

2.借鑒大神的實現方式

public class Demo {

?? ?public static void main(String[] args) {
?? ??? ?System.out.println("開始輸入:");
?? ??? ?//從鍵盤輸入字符串
?? ??? ?Scanner input = new Scanner(System.in);
?? ??? ?//獲取鍵盤輸入的字符串
?? ??? ?String str = input.nextLine();
?? ??? ?System.out.println("結束");
?? ??? ?int myAtoi = myAtoi(str);
?? ??? ?System.out.println(myAtoi);
?? ?}
?? ?
?? ?public static int myAtoi(String str) {
?? ? ? ?// 合法性判斷
?? ? ? ?if (str.isEmpty()) return 0;
?? ? ? ?// 正負號標記
?? ? ? ?int sign = 1;
?? ? ? ?// 轉換值
?? ? ? ?int base = 0;
?? ? ? ?// 索引位數
?? ? ? ?int i = 0;
?? ? ? ?// 剔除開始空白字符
?? ? ? ?while (str.charAt(i) == ' ')
?? ? ? ? ? ?i++;
?? ? ? ?// 判斷正負號
?? ? ? ?if (str.charAt(i) == '-' || str.charAt(i) == '+')
?? ? ? ? ? ?sign = str.charAt(i++) == '-' ? -1 : 1;
?? ? ? ?// 索引有效數字字符
?? ? ? ?while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
?? ? ? ? ? ?// that statement is used to test if the num is bigger than INT_MAX after the str[i] is handled, if base > INT_MAX/10,?
?? ? ? ? ? ?// then base10+ str[i] -‘0’> base10>INT_MAX, or when base== INT_MAX/10, that means all the places are the same as INT_MAX except the ones place, so str[i]>‘7’ is needed.?
?? ? ? ? ? ?// 上面這段是LeetCode國外站對下面代碼的解釋。
?? ? ? ? ? ?// 簡單來說就是
?? ? ? ? ? ?// 如果`base > MAX_VALUE/10`,那么`base*10 + new_value` > `base*10` > `MAX_VALUE`。這個應該很容易理解,這種情況下就會發生溢出。
?? ? ? ? ? ?// 若`base == INT_MAX/10`,而且`new_value = str.charAt(i++) - '0'`大于`7`,也會發生溢出。因為`MAX_VALUE = 2147483647`
?? ? ? ? ? ?if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
?? ? ? ? ? ? ? ?return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
?? ? ? ? ? ?}
?? ? ? ? ? ?// 計算轉換值,str.charAt(i++) - '0'可以把字符串轉換成數字
?? ? ? ? ? ?base = 10 * base + (str.charAt(i++) - '0');
?? ? ? ?}
?? ? ? ?// 計算結果值
?? ? ? ?return base * sign;
?? ?}

注意:判斷字符串的值是否超出32位有符號數的范圍的思路:

1.把字符串轉換成字符數組,然后依次獲取每個字符,定義一個起始值int為0的變量來代表獲取到的字符base,定義一個int為1的變量來代表字符的符號sign。判斷base是否大于整數的最大值除以10,或者判斷base如果=整數的最大值除以10,并且獲取到的當前字符的值大于7,如果是,判斷base的符號,如果是“-”,則返回整數的最小值,否則返回整數的最大值;如果base的大小在32位整數范圍內,則返回base*sign;

總結

以上是生活随笔為你收集整理的从键盘获取字符串,并把字符串转数字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。