Title: Imitate the Javaapparguments.java instance, write a program that accepts multiple numbers from the command line, and then outputs the sum.
1. Design Ideas
The parameters you enter are strings, and you want to make them into numbers before you add them. Because you do not know how many parameters to enter, enter n for the For loop. Finally, the sum of these numbers is entered and the final sum is obtained.
2. Program Flowchart
Define the number of arguments n and enter n-----------> enter the number, and for loop convert the input string to a number-----------> then the For loop adds these numbers-------------> the results from the final output.
3. SOURCE program code
Import Java.util.Scanner;
public class Arguments {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Scanner reader=new Scanner (system.in);
int n=0;
System.out.println ("A total of several parameters summed, please enter the number:");
N=reader.nextint ();
Int[] Aintarray;
Aintarray=new Int[n];
System.out.println ("Please enter the sum of the numbers:");
for (int i=0;i<aintarray.length;i++)
{
Aintarray[i]=reader.nextint ();
}
int sum=0;
for (int i=0;i<aintarray.length;i++)
{
Sum+=aintarray[i];
}
System.out.println ("The sum of these numbers is:" +sum);
}
}
4. Results
For a total of several parameters, enter the number:
6
Please enter the sum of the numbers:
1 2 3 4 5 6
The sum of these numbers is: 21
A Java applet that adds results to multiple numbers