Java Development Yard


Latest Article

Another class to calculate the average of numbers

This is a class to calculate the average of numbers

import javax.swing.JOptionPane;
public class Average {
	public static void main(String args[]) {
		int sum = 0, n = 0, score;
		double avg;
		String scoreString;
		scoreString = JOptionPane.showInputDialog("Please input a number:");
		score = Integer.parseInt(scoreString);
		while (score != -1) {
			sum = sum + score;
			n = n + 1;
			scoreString = JOptionPane
					.showInputDialog("Please input a number: ");
			score = Integer.parseInt(scoreString);
		}
		if (n > 0) {
			avg = (double) sum / n;
			JOptionPane.showMessageDialog(null, n + "Numbers' Avage: " + avg,
					" ", JOptionPane.INFORMATION_MESSAGE);
		} else
			JOptionPane.showMessageDialog(null, "You didn't enter any number!"
					+ "", "You didn't enter any number!",
					JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
}

Latest Articles

Login Form