//1. Enter an integral type from the keyboard (similar to other basic types)Scanner sc =NewScanner (system.in); Sc.hasnextint ();intSTR1 =sc.nextint ();//2. Enter a fixed-length integer array from the keyboardint[] arr =New int[21]; Scanner SC=NewScanner (system.in); for(inti=0;i<21;i++) {sc.hasnextint (); Arr[i]=sc.nextint ();}//3. Enter a string from the keyboard (no spaces)Scanner sc =NewScanner (system.in); Sc.hasnext (); String str1=Sc.next ();//4. Enter a line of string from the keyboard (can contain spaces)Scanner sc =NewScanner (system.in); Sc.hasnextline (); String str1=sc.nextline ();//5. Enter a comma-separated array of integers from the keyboard, with a negative integer and an indefinite length// such as 1,2,-3,4,-5,6 ...
Note Integer.parseint cannot parse negative integertype of string, really pit, at that time this small white exam last together defeated on this, a bitter tearScanner sc =NewScanner (system.in); Sc.hasnextline (); string[] Str= Sc.nextline (). Split (","); int[] arr =New int[Str.length]; for(inti=0;i<str.length;i++){ if(Str[i].startswith ("-") ) {Arr[i]=-integer.parseint (str[i].substring (1)); }Else{Arr[i]=Integer.parseint (Str[i]); } }
is a pit stepping over personal experience, Xiao Xu hope to help the lucky little white people.
Xiao Xu See the world, the world so beauty: http://www.cnblogs.com/schoolbag/
Small white school recruit machine entrance into the pit the various data types of Java input from the keyboard