[Turn]java implementation, input data, space continues, enter end input

Source: Internet
Author: User

Normal version: Can be input, can be output. With detailed notes

1 ImportJava.util.Scanner;2  3  Public classSumdemo {4      Public Static voidMain (string[] args) {5System.out.println ("Please enter two digits, separated by a space, for example 5 5");6         //get a scanner to scan the system for input7Scanner input =NewScanner (system.in);8         //declares a temporary string variable temp, which is used to hold a line read by the scanner;9String temp =input.nextline ();Ten         //The temp string is first trimmed () to remove the blank space on both sides. One         //because some people may enter a space 5 space 5 Space carriage return: A         //so remove both sides of the space into 5 space 5 back to meet the requirements of the car -         //The split ("") method means that the string is cut with a space, and the returned result is an array of strings -string[] ss = Temp.trim (). Split (""); the         //parse from two strings to get two numbers and sum -         intNUM1 = Integer.parseint (ss[0]); -         intnum2 = Integer.parseint (ss[1]); -         intsum = num1+num2; +         //Output Results -System.out.println ("The number entered is" +num1+ "" +num2+ "two number of the and is:" +sum); +         //Develop good habits, open the resources to remember to close, we opened the scanner, we need to close the scanner A input.close (); at     } -}

Upgrade: Can re-enter numbers, repeat the output, and with the Exit function,

1 ImportJava.util.Scanner;2  3  Public classSumtest {4      Public Static voidMain (string[] args) {5Scanner input =NewScanner (system.in);6          while(true){7System.out.println ("If you enter exit, exit.") Enter two digits, separated by a space ");8String temp =input.nextline ();9             if(Temp.trim (). Equals ("Exit")){Ten                  Break; One             } Astring[] ss = Temp.trim (). Split (""); -             intNUM1 = Integer.parseint (ss[0]); -             intnum2 = Integer.parseint (ss[1]); the             intsum = num1+num2; -System.out.println ("The number entered is" +num1+ "" +num2+ "two number of the and is:" +sum); -         } - input.close (); +     } -   +}

From Baidu know

Https://zhidao.baidu.com/question/367772921046984684.html

[Turn]java implementation, input data, space continues, enter end input

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.