[Image]

Counter

Counts odd numbers, from 1 to 99, adding up all the number.  Uses a List box for output.


Important Commands

List   nums = new List(15);
Create a List box to be used for output.

nums.add(c + "");
Add number c to the nums List.  Must use  c + ""  to change number to a String before adding.

for (int c=1; c < 100; c = c + 2)
Loop counts from 1 to 99, by 2 (e.g. odd numbers).

total = total + c;
Adds up the c numbers.

System.exit(0);
Ends the program.


Comments

There is only one button, so the actionPerformed method only needs to respond to one button (e.g. no  if.. command needed).

The counting loop is directly inside the constructor, so it runs immediately when the program starts.  This is normally not sensible.