2014-07-02 71 views
-1

我正在爲我的編程課程介紹做一個簡單的骰子滾動類遊戲,我需要一些幫助,我的隨機數發生器。每次使用它們時,他們都會遵循相同的數字序列,從而使每個卷都成爲一條平行線,有沒有辦法解決這個問題?在VB.Net 2010中的RNG

Module Module1 

    Sub Main() 
     Dim rnumber1 As Random 
     Dim rnumber2 As Random 
     Dim number1 As Integer 
     Dim number2 As Integer 
     Dim user1 As String 
     Dim user2 As String 
     Dim score1 As Integer 
     Dim score2 As Integer 
     rnumber1 = New Random 
     number1 = rnumber1.Next(1, 6) 
     rnumber2 = New Random 
     number2 = rnumber2.Next(1, 6) 
     Console.WriteLine("Welcome to my dice game!") 
     Console.WriteLine("The objective of the game is to roll higher than your opponent.") 
     Console.WriteLine("The first one to 10 points wins!") 
     Console.ReadLine() 
     Console.Clear() 
     Console.WriteLine("First player, please enter your name.") 
     user1 = Console.ReadLine() 
     Console.WriteLine("Thank you, {0}!", user1) 
     Console.ReadLine() 
     Console.Clear() 
     Console.WriteLine("Second player, please enter you name.") 
     user2 = Console.ReadLine() 
     Console.WriteLine("Thank you, {0}!", user2) 
     Console.ReadLine() 
     Console.Clear() 
     Do Until score1 Or score2 = 10 
      rnumber1 = New Random 
      number1 = rnumber1.Next(1, 6) 
      rnumber2 = New Random 
      number2 = rnumber2.Next(1, 6) 
      Console.Clear() 
      Console.WriteLine("The score so far is {0} to {1}!", score1, score2) 
      Console.WriteLine("Press enter to continue...") 
      Console.Clear() 
      Console.WriteLine("{0}, press enter to roll the dice!", user1) 
      Console.ReadLine() 
      Console.WriteLine(number1) 
      Console.ReadLine() 
      Console.WriteLine("{0}, press enter to roll the dice!", user2) 
      Console.ReadLine() 
      Console.WriteLine(number2) 
      Console.ReadLine() 
      If number1 > number2 Then 
       Console.WriteLine("The winner of this round is {0}.", user1) 
       Console.ReadLine() 
       score1 = score1 + 1 
       Console.Clear() 
      ElseIf number2 > number1 Then 
       Console.WriteLine("The winner of this round is {0}.", user2) 
       Console.ReadLine() 
       score2 = score2 + 1 
       Console.Clear() 
      Else : Console.WriteLine("This round was a tie!") 
       Console.ReadLine() 
      End If 
     Loop 

    End Sub 

End Module 
+1

Visual Studio不是一種編程語言;它是一個IDE。由於你的問題不是關於Visual Studio的使用,所以我刪除了該標籤併爲VB.Net添加了正確的標籤。如果你要學習編程,至少應該學會識別你將要使用的語言。 :-) –

+2

創建和使用只有一個「Random」實例,而不是兩個:用'rnumber1'替換'rnumber2',並且不要多次調用'New Random'。 –

+0

對不起,我一直這樣做了大約一個星期,所以這對我來說還是很新鮮和令人困惑的! Michael,應該用rnumber1替換rnumber2,還是你的意思是用它替換所有的實例? – Jason

回答

0

我解決了你的問題。

下面是代碼,試試吧:

Sub Main() 
    Dim rnumber1 As Random 
    Dim rnumber2 As Random 
    Dim number1 As Integer 
    Dim number2 As Integer 
    Dim user1 As String 
    Dim user2 As String 
    Dim score1 As Integer 
    Dim score2 As Integer 
    Console.WriteLine("Welcome to my dice game!") 
    Console.WriteLine("The objective of the game is to roll higher than your opponent.") 
    Console.WriteLine("The first one to 10 points wins!") 
    Console.ReadLine() 
    Console.Clear() 
    Console.WriteLine("First player, please enter your name.") 
    user1 = Console.ReadLine() 
    Console.WriteLine("Thank you, {0}!", user1) 
    Console.ReadLine() 
    Console.Clear() 
    Console.WriteLine("Second player, please enter your name.") 
    user2 = Console.ReadLine() 
    Console.WriteLine("Thank you, {0}!", user2) 
    Console.ReadLine() 
    Console.Clear() 
    Do Until score1 Or score2 = 10 
     rnumber1 = New Random 
     number1 = rnumber1.Next(1, 6) 
     rnumber2 = New Random 
     number2 = rnumber2.Next(1, 6) 
     rnumber2 = rnumber1 
     number2 = rnumber2.Next(1, 6) 
     Console.Clear() 
     Console.WriteLine("The score so far is {0} to {1}!", score1, score2) 
     Console.WriteLine("Press enter to continue...") 
     Console.Clear() 
     Console.WriteLine("{0}, press enter to roll the dice!", user1) 
     Console.ReadLine() 
     Console.WriteLine(number1) 
     Console.ReadLine() 
     Console.WriteLine("{0}, press enter to roll the dice!", user2) 
     Console.ReadLine() 
     Console.WriteLine(number2) 
     Console.ReadLine() 
     If number1 > number2 Then 
      Console.WriteLine("The winner of this round is {0}.", user1) 
      Console.ReadLine() 
      score1 = score1 + 1 
      Console.Clear() 
     ElseIf number2 > number1 Then 
      Console.WriteLine("The winner of this round is {0}.", user2) 
      Console.ReadLine() 
      score2 = score2 + 1 
      Console.Clear() 
     Else : Console.WriteLine("This round was a tie!") 
      Console.ReadLine() 
     End If 
    Loop 
End Sub 

希望此代碼對您有所幫助。

0

您必須將隨機在編譯時:

Module Module1 

    Dim rnumber1 As New Random 
    Dim rnumber2 As New Random 

    Sub Main() 

     Dim number1 As Integer 
     Dim number2 As Integer 
     Dim user1 As String 
     Dim user2 As String 
     Dim score1 As Integer 
     Dim score2 As Integer 
     number1 = rnumber1.Next(1, 6) 
     number2 = rnumber2.Next(1, 6) 
     Console.WriteLine("Welcome to my dice game!") 
     Console.WriteLine("The objective of the game is to roll higher than your opponent.") 
     Console.WriteLine("The first one to 10 points wins!") 
     Console.ReadLine() 
     Console.Clear() 
     Console.WriteLine("First player, please enter your name.") 
     user1 = Console.ReadLine() 
     Console.WriteLine("Thank you, {0}!", user1) 
     Console.ReadLine() 
     Console.Clear() 
     Console.WriteLine("Second player, please enter you name.") 
     user2 = Console.ReadLine() 
     Console.WriteLine("Thank you, {0}!", user2) 
     Console.ReadLine() 
     Console.Clear() 
     Do Until score1 Or score2 = 10 
      rnumber1 = New Random 
      number1 = rnumber1.Next(1, 6) 
      rnumber2 = New Random 
      number2 = rnumber2.Next(1, 6) 
      Console.Clear() 
      Console.WriteLine("The score so far is {0} to {1}!", score1, score2) 
      Console.WriteLine("Press enter to continue...") 
      Console.Clear() 
      Console.WriteLine("{0}, press enter to roll the dice!", user1) 
      Console.ReadLine() 
      Console.WriteLine(number1) 
      Console.ReadLine() 
      Console.WriteLine("{0}, press enter to roll the dice!", user2) 
      Console.ReadLine() 
      Console.WriteLine(number2) 
      Console.ReadLine() 
      If number1 > number2 Then 
       Console.WriteLine("The winner of this round is {0}.", user1) 
       Console.ReadLine() 
       score1 = score1 + 1 
       Console.Clear() 
      ElseIf number2 > number1 Then 
       Console.WriteLine("The winner of this round is {0}.", user2) 
       Console.ReadLine() 
       score2 = score2 + 1 
       Console.Clear() 
      Else : Console.WriteLine("This round was a tie!") 
       Console.ReadLine() 
      End If 
     Loop 

    End Sub 

End Module 

我希望它的工作。這是因爲隨機使用自己的時間。