ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {intNextValue; intSum=0; Scanner Kbinput=NewScanner (system.in); Kbinput.usedelimiter ("\\s");//Specify a space as a delimiter while(Kbinput.hasnextint ()) {//Judging if there are no integers to readNextValue =Kbinput.nextint (); Sum+=NextValue; } System.out.println ("Sum:" +sum);
System.out.printf ("sum:%d", Sum); Kbinput.close (); //Close Stream Object}}
The results of the operation are as follows:
5 5 5 5
Sum:20
Sum:20
Enter any number, with a space to separate each number, press ENTER will automatically find the number of the and.
This assignment mainly studied the Usedelimiter in scanner. This function is used to set the delimiter, this time the job with a space as a delimiter ("\\s").
Also learned the Hasnextint in scanner, which is used to determine whether there is an integer readable.
The work in the teacher's classroom to explain the help, the use of new knowledge, the successful completion of this assignment!
==================================================================================
-----------------------------------------Split Line----------------------------------------
==================================================================================
After the discussion of our group Gao Yunpeng, Zheng, Zhang Cheng and Kezehui, our final works are ready to be made according to the three-person fighting landlord.
Interface preliminary plan.
Third assignment: Learning from Scanner