A Java applet that adds results to multiple numbers

Source: Internet
Author: User

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

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.