Java Course assignment--parameter summation

Source: Internet
Author: User

First, design ideas:

This program is the use of parameters to input, to achieve a multiple values can be entered at the same time, the number of input is not limited (the parameter is greater than 0, if 0, you should output a hint input parameters). This procedure is divided into steps: 1, the use of parameters to enter the number of inputs to be added, there are two methods: (1) using the debug Configurations-->arguments-->program in eclipse Input parameters in arguments (separated by a space between the parameters). (2) using the cmd Java file name parameter, enter the number to be added, for example: Java ADD 1 2 3 4 5 6 7. 2, the number of parameters to judge (using the IF statement to determine the number of parameters), if the number of parameters is zero, then output please input parameters of the statement to prompt. 3, the number of output parameters, the use of args.length. 4. Define a variable of type int (with an initial value of 0) for calculating the parameters and using the For loop to find the parameters and. (a cast of type int is required, such as: Integer.parseint (ARG)) 5, output the values of each parameter in a for loop, and "+", be aware that the IF statement should be used to differentiate when the last parameter is output, no more output "". 6, output parameters and.

Second, the Process flow chart:

  

Third, the source program code:

Parameter summation.
Zhaoxuan Li,september 26th,2016.

public class Add
{
public static void Main (string[] args)
{
if (args.length==0)
{
System.out.println ("Please enter the parameter in the parameter line (the number to be added)!" ");
}
Else
{
System.out.println ("Number of parameters:" +args.length);
int he=0,jb=0;
for (String Arg:args)
{
He+=integer.parseint (ARG);
jb++;
if (jb!=args.length)
{
System.out.print (arg+ "+");
}
Else
{
System.out.print (ARG);
}
}
System.out.print ("=" +he);
}
}
}

Iv. results:

Java Course assignment--parameter summation

Related Article

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.