مشكلة4....+الحل

Write a java program that read integers and find the total and average of the input values. End your program with the input

Solution:

import javax.swing.JOptionPane;
/** * * @author Administrator */

public class Main {
/** Creates a new instance of Main */

public Main() { }
/** * @param args the command line arguments */

public static void main(String[] args) {

int sum=0;

//Get the number of input elements

int n=Integer.parseInt(JOptionPane.showInputDialog("please enter how many numbers u want"));

//loop for each element

for(int i=0;i

//get the input number, convert it to integer

int x=Integer.parseInt(JOptionPane.showInputDialog("enter number"));

//add number to the summation

sum+=x;

}

//Calculate average

float average=sum*1.0f/n;

//Show average and summation

JOptionPane.showMessageDialog(null,"average=" +average);

JOptionPane.showMessageDialog(null,"sum=" +sum); }
}

0 comments: