Source: http://www.imooc.com/video/1626
Use the Scanner tool class to get user-entered score information
The scanner class is located in the Java.util package and needs to be imported when using the package
Steps:
1. Import Java.util.Scanner
2. Create a Scanner object
3. Receive and save user-entered values
The original code:
1 PackageCom.imooc;2 3 Public classHelloWorld {4 Public Static voidMain (string[] args) {5 //Variable Save score6 intScore = 53; 7 8 //variables are saved in addition to the number of times9 intCount = 0;Ten One //print output plus pre-score ASystem.out.println ("Pre-bonus score:" +score); - - //as long as the score is less than 60, the loop performs the add-on operation and counts the number of points the for(; score + count <; count++) { - - } - + //print output plus score, plus number of points -System.out.println ("Bonus score:" + (score +count)); +System.out.println ("A total of" +count+ "Times!")); A System.out.println (); at } -}
Needs to be changed to allow users to enter scores, change to the following
1 PackageCom.imooc;2 ImportJava.util.Scanner;//written behind the package3 4 Public classHelloWorld {5 Public Static voidMain (string[] args) {6Scanner input=NewScanner (system.in);//Create a Scanner object7System.out.println ("Please enter exam results information:");8 9 intScore = Input.nextint ();//get user-entered scores and save them in a variableTen One //variables are saved in addition to the number of times A intCount = 0; - - //print output plus pre-score theSystem.out.println ("Pre-bonus score:" +score); - - //as long as the score is less than 60, the loop performs the add-on operation and counts the number of points - for(; score + count <; count++) { + - } + A //print output plus score, plus number of points atSystem.out.println ("Bonus score:" + (score +count)); -System.out.println ("A total of" +count+ "Times!")); - System.out.println (); - } -}
The result is (the input is 47):
The total was added 13 times!
From the above, the message "Please enter exam results information:" and user input is not the same line
What if the modification can be on the same line?
Can put System.out.println ("Please enter test results information:"); change the println into print.
The difference is that the println output wraps, and print does not wrap.
The results are as follows:
Please enter your exam results information: 7 total plus !
Web-android Engineer Beginner-5-3 Programming exercise optimization