Java programming: string to numeric summation

Source: Internet
Author: User

Program to access numbers from the command line, sum, if the access is a string of solutions

Design ideas:

First define a double type of a one-dimensional array, and then define a double variable sum, and assign a value of 0, with the IF loop to determine if there is a parameter input from the command line, if not the output command line without parameter input, end the program, if there is, The string is converted to a double type with a For loop, and the output is summed with sum

Process The sequence flowchart is:

The program source code is:

Package demo;
Import Java.util.Scanner;
public class Arguments {

public static void Main (string[] args) {
TODO auto-generated method stubs
Double []a=new double[20];
Double sum=0;
if (args.length==0)//No arguments are entered into the command line
SYSTEM.OUT.PRINTLN ("command line without parameter input");
Else
{
for (int i=0;i<args.length;i++)//input parameters to the command line
{
A[i]=double.parsedouble (Args[i]); Convert to double type
Sum=sum+a[i];
}
System.out.println ("Sum of:" +sum); Output sum
}

}

}

The program is:

Java programming: string to numeric summation

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.