Course Assignment 01 Add operation

Source: Internet
Author: User

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

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.