FileDocCategorySizeDatePackage
addRandom.javaAPI DocExample1162Wed Aug 02 13:23:14 BST 2000None

addRandom

public class addRandom extends Applet

Fields Summary
private int
stopValue
private int
counter
private int
sum
private double
average
Constructors Summary
Methods Summary
public voidpaint(java.awt.Graphics g)

		stopValue = (int) (Math.random() * 20 + 1); // loop termination value
		counter   = 1;				// loop starts from 1
		sum       = 0;				// set sum to 0 first
		while (counter <= stopValue)
		{
			sum = sum + counter;
			g.drawString("counter is " + counter, 20, counter * 10 + 20);
			counter = counter + 1; // add 1 to the loop control variable
		}
		average = (double)sum / stopValue;
		g.drawString("the Sum of the " + stopValue + " numbers is " + sum, 
							20, counter * 10 + 40);
		g.drawString("the Average of the " + stopValue + " numbers is " + average, 
							20, counter * 10 + 60);