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