Web server info - Chapter 7 Arrays 245 quency to determine which
Chapter 7 Arrays 245 quency to determine which element should be incremented during each iteration of the loop, replaces lines 95 115 of Fig. 6.11. The random number calculation on line 88 produces numbers 1 6 (the values for a six-sided die); thus, the frequency array must be large enough to allow subscript values of 1 6. The smallest number of elements required for an array to have these subscript values is seven elements (subscript values 0 6). In this program, we ignore element 0 of array frequency. Lines 75 80 replace lines 69 81 from Fig. 6.11. We can loop through array frequency; therefore, we do not have to enumerate each line of text to display in the Label, as we did in Fig. 6.11. 1 // Fig. 7.7: RollDie.cs 2 // Rolling 12 dice. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.IO; 11 12 public class RollDie : System.Windows.Forms.Form 13 { 14 private System.Windows.Forms.Button rollButton; 15 16 private System.Windows.Forms.RichTextBox displayTextBox; 17 18 private System.Windows.Forms.Label dieLabel1; 19 private System.Windows.Forms.Label dieLabel2; 20 private System.Windows.Forms.Label dieLabel3; 21 private System.Windows.Forms.Label dieLabel4; 22 private System.Windows.Forms.Label dieLabel5; 23 private System.Windows.Forms.Label dieLabel6; 24 private System.Windows.Forms.Label dieLabel7; 25 private System.Windows.Forms.Label dieLabel8; 26 private System.Windows.Forms.Label dieLabel9; 27 private System.Windows.Forms.Label dieLabel10; 28 private System.Windows.Forms.Label dieLabel11; 29 private System.Windows.Forms.Label dieLabel12; 30 31 private System.ComponentModel.Container components = null; 32 33 Random randomNumber = new Random(); 34 int[] frequency = new int[ 7 ]; 35 36 public RollDie() 37 { 38 InitializeComponent(); 39 } 40 41 // Visual Studio .NET generated code 42 Fig. 7.6 Using arrays to eliminate a switch structure. (Part 1 of 3.)
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.