/** * This program will compute the arithmetic average for a set of non-negative integers . * Enter -1 to terminate the program. * @author Anthony Larrain */ import java.text.DecimalFormat; import javax.swing.JOptionPane; public class Average { public static void main( String [] args ) { int sum = 0, count = 0; String numbers = JOptionPane.showInputDialog("Enter Integer (-1 to quit) "); int value = Integer.parseInt(numbers); while (value != -1){ sum += value; ++count; numbers = JOptionPane.showInputDialog("Enter Integer (-1 to quit) "); value = Integer.parseInt(numbers); } if(count != 0){ double average = sum / (double) count; DecimalFormat fmt = new DecimalFormat("0.000"); System.out.println("The average is : " + fmt.format(average)); }else{ System.out.println("No values entered"); } System.exit(0); } }