Adding numbers with methods – Java

What it does:

A simple Java program for adding number. Prompts the user to enter how many numbers they wish to add. This is within a return method where it returns what the user has entered to enable it to be accessed by the next method. The next method is a non-return (void). The numbers are added via a for loop which takes how many numbers the user wishes to add and runs for that length of time. To ensure that the running total is kept  the numbers entered are assigned to a running total variable. At the end it program it prints out  the total of all the numbers entered.

Code:

 


public class AddingInJava
{
    private static Scanner keyboard = new Scanner(System.in);
    private static int amountOfNumbers =0;
    private static double numbersEntered;
    private static double totallingNumbers;

    public static int howManyNumbers()
    {
        System.out.println("Please enter the amount of numbers you wish to add");
        amountOfNumbers = keyboard.nextInt();

        return amountOfNumbers;
    }//howManyNumbers()

    public static void userInputingAddingNums()
    {
        for (int count =0; count<amountOfNumbers; count++)
        {
            System.out.println("Please enter your " + (count+1) + " number");
            numbersEntered = keyboard.nextDouble();

            totallingNumbers+=numbersEntered;
        }//for

        System.out.println("The total is " + totallingNumbers);
    }//userInputingAddingNums()

    public static void main(String [] args)
    {
        howManyNumbers();
        userInputingAddingNums();
    }//main

}//class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s