校招小白机考入坑之从键盘输入java的各种数据类型
生活随笔
收集整理的這篇文章主要介紹了
校招小白机考入坑之从键盘输入java的各种数据类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//1.從鍵盤輸入一個整型(其他基本類型類似)
Scanner sc =new Scanner(System.in);
sc.hasNextInt();
int str1 = sc.nextInt();//2.從鍵盤輸入一個定長的整型數組
int[] arr = new int[21];
Scanner sc = new Scanner(System.in);
for (int i=0;i<21;i++){
sc.hasNextInt();
arr[i]=sc.nextInt();}//3.從鍵盤輸入一個字符串(沒有空格)
Scanner sc =new Scanner(System.in);
sc.hasNext();
String str1 = sc.next();//4.從鍵盤輸入一行字符串(可以包含空格)
Scanner sc =new Scanner(System.in);
sc.hasNextLine();
String str1 = sc.nextLine();//5.從鍵盤輸入一行以逗號隔開的整型數組,包含負整型,且不定長
// 如 1,2,-3,4,-5,6 ...
// 注意Integer.parseInt不能解析負整型的字符串,真的坑,那時本小白考試最后一道就敗在這上面,一把辛酸淚Scanner sc = new Scanner(System.in);sc.hasNextLine();String[] str = sc.nextLine().split(",");int[] arr = new int[str.length];for (int i=0;i<str.length;i++){if (str[i].startsWith("-")){arr[i]=-Integer.parseInt(str[i].substring(1));}else {arr[i]=Integer.parseInt(str[i]);}}
// 注意Integer.parseInt不能解析負整型的字符串,真的坑,那時本小白考試最后一道就敗在這上面,一把辛酸淚Scanner sc = new Scanner(System.in);sc.hasNextLine();String[] str = sc.nextLine().split(",");int[] arr = new int[str.length];for (int i=0;i<str.length;i++){if (str[i].startsWith("-")){arr[i]=-Integer.parseInt(str[i].substring(1));}else {arr[i]=Integer.parseInt(str[i]);}}
都是一個一個坑踩過來的個人經驗,小徐希望能幫到幸運的小白們。
?
小徐看世界,世界如此多嬌: http://www.cnblogs.com/schoolbag/
轉載于:https://www.cnblogs.com/schoolbag/p/8672068.html
總結
以上是生活随笔為你收集整理的校招小白机考入坑之从键盘输入java的各种数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA不认识jstl
- 下一篇: bzoj1096 [ZJOI2007]仓