2016-03-22 8 views
1

現在のゲームサイクルの推測の順序数を1増やしたいのですが、最初に値を0に設定しましたが、1の後に更新しません。 試行回数は同じです。私は21に値を設定しているが、すぐにそれが20になるまで更新ではなく、あなたのボタンのイベントハンドラでそのVBで変数を更新する

Option Strict On 
Option Explicit On 
Public Class Form1 
    Private ReadOnly rand As New Random 
    Private value As Integer 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 

     Me.value = rand.Next(minValue:=1, maxValue:=30) 

    End Sub 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim guess As Integer  'guess number 
     Dim numTry As Integer  'No. of trys 
     Dim RandNo As Integer  'Random Number 
     Dim OrdNo As Integer  'Ordinal Number 
     Dim Score As Integer  'Score 

     Score = 0 'Initial value of score set as 0 
     OrdNo = CInt(1) 'Initial value of ordinal set as 1 
     numTry = CInt(21) 
     guess = CInt(TextBox1.Text) 
     OrdNo = +1 
     Label5.Text = CStr(OrdNo) 

     'Show Message Box if the guess is not within the range 
     If 1 > guess Then 
      MessageBox.Show("Input within the range (1-30)", "Error", MessageBoxButtons.OK) 
      Exit Sub 
     End If 

     'Show Message Box if the guess is not within the range 
     If guess > 30 Then 
      MessageBox.Show("Input within the range (1-30)", "Error", MessageBoxButtons.OK) 
      Exit Sub 
     End If 

     'Display result and message when guess is larger than the lucky number 
     If guess > Me.value Then 
      Label11.Text = CStr(guess) 
      Label10.Text = "The Lucky Number is smaller than your guess" 
      OrdNo = OrdNo + 1 
      Label5.Text = CStr(OrdNo) 
      numTry = numTry - 1 
      Label4.Text = CStr(numTry) 
     End If 

     'Display result and message when guess is smaller than lucky number 
     If guess < Me.value Then 
      Label11.Text = CStr(guess) 
      Label10.Text = "The Lucky Number is larger than your guess" 
      OrdNo = OrdNo + 1 
      Label5.Text = CStr(OrdNo) 
      numTry = numTry - 1 
      Label4.Text = CStr(numTry) 

     End If 

     'Display result and message when guess is equal to the lucky number 
     If guess = Me.value Then 
      Label11.Text = CStr(guess) 
      Label10.Text = "Congratulations ! This is the lucky number" 
      Score = +10 'Increase the score by 10 
      Label6.Text = CStr(Score) 
      numTry = numTry - 1 
      OrdNo = 1 
      Me.value = rand.Next(minValue:=1, maxValue:=30) 

      If numTry = 0 Then Application.Exit() 

     End If 

    End Sub 

End Class 
+2

そして、何ですあなたの問題?あなたはどんなエラーを出しているのですか?何がうまくいかない? – Claudius

+1

私は彼が正しいと推測するまで番号が同じであることを望んでいると思うが、このコードを見ると、間違って推測されるたびに乱数が変わると思う。 – Werdna

+0

デバッグすることを覚えてください.. – cybermonkey

答えて

2

後、RandNoは常にゼロです。 Form Loadイベントハンドラで宣言されている同じ名前のRandNoの変数の存続期間は、そのメソッドにのみ残ることを考慮してください。

次の手順を実行し、それを避けるために:

まず(しかし、必要に応じて)、(MSDNドキュメントで説明したようにそうしないと、繰り返しの番号を取得することができます)一度だけ、それを初期化するために確保、Randomクラスを使用します。第二に

、行ってその価値と仕事を割り当て、メソッドブロックの範囲外と同じように、一度だけ、ランダムな値を格納します整数変数を宣言:

Public Class Form1 

    Private ReadOnly rand As New Random 
    Private value As Integer 

    Private Sub Form1_Load() Handles MyBase.Load 

     Me.value = rand.Next(minValue:=0, maxValue:=100) 

    End Sub 

    Private Sub Button1_Click() Handles Button1.Click 
     ' Do what you want with Me.value 
     '... 
    End Sub 

End Class 
+0

私はそれをかなり得ません。それでも何も生成しません。もう一度やり直せますか? –

+0

https://www.dropbox.com/s/a9w9ckp3ffwds9i/asg1_test.exe?dl=0このようなゲームを作りたいと思います。 –

+2

申し訳ありませんが、いいえ、私はあなたの仕事をするためにプロジェクトをダウンロードするつもりはありません!私たちはあなたがそれを行う方法を理解するのを助けるためにここにあります、私はあなたが主要な問題を解決する方法を説明しました、疑問が残っている場合は、質問を更新して、作成しているコードに進歩性を加え、私が示したコードに関連する問題を説明することができます。 – ElektroStudios

関連する問題