校招小白机考入坑之从键盘输入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]); } }

都是一个一个坑踩过来的个人经验,小徐希望能帮到幸运的小白们。

小徐看世界,世界如此多娇: http://www.cnblogs.com/schoolbag/