Web-android Engineer Beginner-5-3 Programming exercise optimization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.