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);