Monday, April 9, 2007

Average of Numbers

import javax.swing.*;

public class Average{
public static void main(String[] args){
Average ave=new Average();
}
public Average(){
String output="",input,inputs;
float num,sum=0,nums,ave;
input=JOptionPane.showInputDialog("Enter how many numbers to compute:");

num=Float.parseFloat(input);
for (int x=1;x<=num;x++)
{
inputs=JOptionPane.showInputDialog("Enter a number :");
nums=Float.parseFloat(inputs);
sum=sum+nums ;
}
ave=sum/num;
output+="The average is: "+ ave;
JOptionPane.showMessageDialog(null,output,"Average",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

No comments: