1, the purpose of the experiment
Write a program that allows you to accept multiple numbers from the command line, summing and outputting the results
2. Design Ideas
Enter multiple strings, divide them into spaces, divide them into several numbers, output all the elements of the array through a for loop, and convert the string to an integer in another for loop, then sum the result, convert the result to a string type, and finally output.
3. Program Flowchart
5. Program source Code
1 Packagedemo;2 3 /*Experiment Objective: To write a program that allows multiple numbers to be accepted from the command line, and the result is summed.4 Shijiazhuang Railway University letter 1403-2 class possessing5 Date: September 25, 2015*/6 7 Importjava.util.*;//use the class in the Java.util package scanner8 9 Public classSum {Ten One Public Static voidMain (string[] args) { A //TODO Auto-generated method stubs - -Scanner sc =NewScanner (system.in);//constructs a Scanner object instance theSYSTEM.OUT.PRINTLN ("Input:"); -String Str=sc.nextline ();//Accepting console input -String num[]=str.split ("");//Divide the space that is encountered -SYSTEM.OUT.PRINTLN ("Array is:"); + - inti; + for(i=0;i<num.length;i++) A { atSystem.out.println (num[i]+ "");//Output Array - } - - intsum=0;//initialization sum result is 0 - - for(i=0;i<num.length;i++) in { -Integer k=NewInteger (Num[i]);//Convert A string to an integer to intf=K.intvalue (); +Sum+=f;//sum - } theString it=string.valueof (sum);//convert integers to Strings *SYSTEM.OUT.PRINTLN ("result is" + sum);//Output Results $ }Panax Notoginseng}
6, results
Course Assignment 01 Add operation