The following code is used in the after-class exercise
1 Public Static voidMain (string[] args) {2System.out.print ("Enter student number:");3 intStudent_number =input.nextint ();4System.out.print ("Input student Score:");5String scores =input.nextline ();6 int[] Score_arrayi =Getscore (scores);7 intMax_score =Maxscore (score_arrayi);8 grade (Score_arrayi,max_score);9}
However, the following problems may occur in the specific input
That is, after you enter the number of Students 4, press ENTER. Unable to enter student scores, skip directly
1 String scores = Input.nextline ();
After finding the information, I learned that:
Next () must be read to a valid character before the end of the input, to enter a valid character before the space bar, tab or enter key, such as The Terminator, the next () method will automatically remove it, only after entering a valid character, the next () method will be entered after the space bar, The TAB key or enter key is treated as a delimiter or terminator. Simply put, next () finds and returns the next complete tag from this scanner. The complete tag is preceded by the input information that matches the delimited pattern, so the next method cannot get a string with spaces
The Terminator of the Nextline () method is just the ENTER key, which means that the nextline () method returns all characters before the ENTER key, which is a string with spaces.
This means that nextint () ends after reading a valid value, the carriage return is still present after the number is lost, and nextline () reads the carriage return directly to the end, so the result is similar to skipping the input statement
Therefore, in order to solve this problem, you can add a nextline () after Nextint () to complete the code as follows
1 Public Static voidMain (string[] args) {2System.out.print ("Enter student number:");3 intStudent_number =input.nextint ();4System.out.print ("Input student Score:");5 input.nextline ();6String scores =input.nextline ();7 int[] Score_arrayi =Getscore (scores);8 intMax_score =Maxscore (score_arrayi);9 grade (Score_arrayi,max_score);Ten}
Questions about nextline () that cannot be entered in Java