Java #8: Average of User Inputted Numbers(Loop until user wants to quit)

//Programmer:Akinwale Owi
//Program Purpose: Average, Loop Until User Enters A Negative
//Company:Dagba Computers
//——————————————————

//package name
package dagba.computers;

//imports
import java.util.Scanner;

//class name
public class DagbaComputers {

//main function
public static void main(String[] args)
{

//Creation of Scanner
Scanner numScanner = new Scanner (System.in);

//Variables to save user input
double firstNum, secondNum, total,average,counter;

//counter value starts at 1, because we ask for number outside of the loop//
counter = 1;

//Asking for first number
System.out.println(“Please enter the first number you want me to add to the average:”);

//Reading in firstNum value
firstNum = numScanner.nextDouble();

//Printing out the first number to check
System.out.println(“First Number = “+firstNum);

//Putting the first value into total//
total = firstNum;

//Loop that asks user for number and adds to total
do
{

//Asking for second number//
System.out.println(“Please enter the next number you want to add or enter negative number to quit:”);

//Inputting second number value
secondNum = numScanner.nextDouble();

if (secondNum >-1)
{
total = total + secondNum;
counter = counter + 1;
}

else
{
//Printing a space for readability
System.out.println(“”);

//Printing Results Heading
System.out.println(“———Results———-“);

//Printing out the total of all numbers entered expect the loop breaker
System.out.println(“Number total = “+total);

//Printing out the total number of numbers entered aka going into the average
System.out.println(“The total number of entries = “+counter);

//Calculating the average//
average = total/counter;

//Printing the average
System.out.println(“The average = “+average);

}//End of else, when they want to break the loop

}while (secondNum > -1);//end of do while loop

}//End of main function

}//end of DagbaComputers Class

 

———————————-will get you this———————————————

 

 

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: