182 CHAPTER 6 MAKING DECISIONS WITH CONDITIONAL (Web hosting solutions)
182 CHAPTER 6 MAKING DECISIONS WITH CONDITIONAL LOGIC Dim r as New Random ‘Instantiate a Random Object intSecretNumber = r.InRange(1,100) ‘Generate a random number The first statement uses the New keyword to instantiate a Random object. The second statement uses the Random class s InRangemethod to randomly generate a number from 1 and 100, and to store it in the intSecretNumberproperty. Note The Random class is a built-in REALbasic class from which you can instantiate a new object based on that class. Classes and objects have not been covered yet, so for now, just enter these code statements as shown. You learn about classes and objects in Chapter 8. Together, these two statements generate an initial secret number the player must guess to win the game. Next, switch back to the Window Layout view of the Windows Editor, and then double-click the Slider control. REALbasic responds by opening the ValueChangedSubroutine for the Slider control. Enter the following code statements. intNoGuessed = intNoGuessed + 1 ‘Increment variable value by 1 edfNoOfGuesses.Text = CStr(intNoGuessed) ‘Display the number of guesses If sdrControl.Value = intSecretNumber Then ‘Check to see if the player won edfAnalysis.Text = “You Win!” ‘Update game status sdrControl.Enabled = False ‘Disable access to the Slider control btnPlay.Enabled = True ‘Enable access to the Play button ‘Display the total number of games played edfGamesPlayed.Text = CStr(Val(edfGamesPlayed.Text) + 1) ‘Calculate the total number of turns taken since the game was started intTotalNoOfTurns = intTotalNoOfTurns + intNoGuessed ‘Calculate the average number of turns per game (as an Integer value) edfAvgNoOfTurns.Text = CStr(intTotalNoOfTurns \ Val(edfGamesPlayed.Text)) End If
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.