Get the Average of numbers in Java

Download
Download is available until [expire_date]
  • Version
  • Download 27
  • File Size 3.55 KB
  • File Count 1
  • Create Date May 10, 2016
  • Last Updated May 10, 2016

Get the Average of numbers in Java

Get the Average of numbers in Java

Average is getting first the sum of numbers, let’s say 10 number, and then divide it to the number of numbers, it would be 10 based on our example. This is a program that will ask the user how many number he or she wants then the program will sum it up and get the average and display the result.

Source code:

import javax.swing.*;
public class averageinjavaGUI
{ 
public static void main(String args[]) {

String cont="n";

do
{
String input1, input2; 
int numToCompute=0; 
double result=0, numbers=0, getAverage=0;

input1 = JOptionPane.showInputDialog(null,"Enter a number : ");	
numToCompute=Integer.parseInt(input1);

for (int x=1; x<=numToCompute; x++){
	int y =x;
	input2 = JOptionPane.showInputDialog(null,"Enter number: " + y-- );	
	numbers=Double.parseDouble(input2);
	result=result +  numbers;
	}

getAverage= result / numToCompute;

JOptionPane.showMessageDialog(null,"Average: " + getAverage  );
	
	cont =	JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
	}while (cont.matches("y"));
	JOptionPane.showMessageDialog(null,"Program closing"  );

	}
}
, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.