The program receives multiple numbers from the command line and outputs the result after summing

Source: Internet
Author: User

Design ideas: Get the number of inputs, deposit a new dynamic array, and then convert the number in the dynamic array from string to int using the Integer.parseint () function to add the output.

Program Flowchart: Get the number of inputs---New dynamic array, enter the number of inputs into a dynamic array, and establish an int variable s=0-> using the Integer.parseint () function to convert and add the output;

SOURCE program code:

Import java.util.ArrayList;

public class D1

{

@SuppressWarnings ({"Unchecked", "Rawtypes"})

public static void Main (string[] args)

{

int A;

a=args.length;//get the number of inputs

ArrayList a1=new//Creating dynamic arrays

ArrayList (); for (int i=0;i<a;i++)//To deposit the entered number into a dynamic array

A1.add (Args[i]);

int s=0;

for (int i = 0;i<a;i++)

{

String b= (String) a1.get (i);

int C;

C=integer.parseint (b);//convert string to int type

s=s+c;//Add sum

}

System.out.println (s);//Output

}

}

Program:

The program receives multiple numbers from the command line and outputs the result after summing

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.