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