2011-12-10 19 views
0

このプログラムを動作させるのに問題があります。他の関数/プロシージャで使用されるメインプログラムに出力されたGenerateNumbersの結果を取得する方法がわかりません。私は配列と関数の問題

Dim Result1 As String = ThreeResults(0) 
Dim Result2 As String = ThreeResults(1) 
Dim Result3 As String = ThreeResults(2) 

を試してみましたが、何らかの理由でそれが各ThreeResultsの値を設定します(X)「を」

Module Module1 

Sub Main() 
    'Declare Variables 
    Dim UserAnswer As String = "nothing" 
    Dim UserTokens As String 
    Dim ThreeResults(2) As String 
    Dim Result1 As String = ThreeResults(0) 
    Dim Result2 As String = ThreeResults(1) 
    Dim Result3 As String = ThreeResults(2) 

    'Ask user for number of tokens 
    Console.WriteLine("Enter the number of tokens that you want to gamble with (minimum of 1 token)") 
    UserTokens = (Console.ReadLine()) 

    UserTokens = ValidateTokens(UserTokens) 

    Do Until UserAnswer = "no" 
     ThreeResults = GenerateNumbers() 

     UserAnswer = DisplayResults(UserTokens, UserAnswer, Result1, Result2, Result3) 
    Loop 

End Sub 

Function ValidateTokens(ByVal UserTokens) 

    Dim dblUserTokens As Double 

    Do Until IsNumeric(UserTokens) 
     Console.Write("Sorry you did not enter a valid number. Please try again. ") 
     UserTokens = Console.ReadLine() 
    Loop 

    'Make sure the user entered in a positive number 
    dblUserTokens = CDbl(UserTokens) 
    Do Until dblUserTokens > 0 
     Console.Write("Sorry you did not enter in a positive number. Please try again. ") 
     UserTokens = Console.ReadLine() 

     'Make sure the user entered in a number 
     Do Until IsNumeric(UserTokens) 
      Console.Write("Sorry you did not enter a valid number. Please try again. ") 
      UserTokens = Console.ReadLine() 
     Loop 
     dblUserTokens = CDbl(UserTokens) 


    Loop 

    Return UserTokens 
End Function 

Function GenerateNumbers() 
    Dim results(2) As String 
    Dim names(5) As String 
    Dim x As Integer 

    names(0) = "Cherries" 
    names(1) = "Oranges" 
    names(2) = "Plums" 
    names(3) = "Bells" 
    names(4) = "Melons" 
    names(5) = "Bar" 

    For x = 0 To 2 
     results(x) = names(CInt(Int((5 * Rnd()) + 1))) 
    Next x 

    Return results 
End Function 



Function DisplayResults(ByRef UserTokens, ByVal UserAnswer, ByRef number1, ByRef number2, ByRef number3) 
    Console.WriteLine("*** " & number1 & " ** " & number2 & " ** " & number3 & " ***") 
    If UserTokens > 0 Then 
     Console.WriteLine("You have " & UserTokens & " tokens left, do you want to play again? (yes or no)") 
     UserAnswer = (Console.ReadLine()) 
    Else 
     Console.WriteLine("Sorry, you lost all of your tokens!") 
    End If 

    Return UserAnswer 
End Function 

エンドモジュール

答えて

1

あなただけで、文字列の値を設定していますあなたのメインルーチンの開始。 ThreeResultsが設定された後で設定する必要があります。文字列はimmutableオブジェクトなので、コードは機能しません。

Module Module1 

Sub Main() 
    'Declare Variables 
    Dim UserAnswer As String = "nothing" 
    Dim UserTokens As String 
    Dim ThreeResults(2) As String 
    Dim Result1 As String 
    Dim Result2 As String 
    Dim Result3 As String 

    'Ask user for number of tokens 
    Console.WriteLine("Enter the number of tokens that you want to gamble with (minimum of 1 token)") 
    UserTokens = (Console.ReadLine()) 

    UserTokens = ValidateTokens(UserTokens) 

    Do Until UserAnswer = "no" 
     ThreeResults = GenerateNumbers() 
     Result1 = ThreeResults(0) 
     Result2 = ThreeResults(1) 
     Result3 = ThreeResults(2) 

     UserAnswer = DisplayResults(UserTokens, UserAnswer, Result1, Result2, Result3) 
    Loop 

End Sub 

Function ValidateTokens(ByVal UserTokens) 

    Dim dblUserTokens As Double 

    Do Until IsNumeric(UserTokens) 
     Console.Write("Sorry you did not enter a valid number. Please try again. ") 
     UserTokens = Console.ReadLine() 
    Loop 

    'Make sure the user entered in a positive number 
    dblUserTokens = CDbl(UserTokens) 
    Do Until dblUserTokens > 0 
     Console.Write("Sorry you did not enter in a positive number. Please try again. ") 
     UserTokens = Console.ReadLine() 

     'Make sure the user entered in a number 
     Do Until IsNumeric(UserTokens) 
      Console.Write("Sorry you did not enter a valid number. Please try again. ") 
      UserTokens = Console.ReadLine() 
     Loop 
     dblUserTokens = CDbl(UserTokens) 


    Loop 

    Return UserTokens 
End Function 

Function GenerateNumbers() 
    Dim results(2) As String 
    Dim names(5) As String 
    Dim x As Integer 

    names(0) = "Cherries" 
    names(1) = "Oranges" 
    names(2) = "Plums" 
    names(3) = "Bells" 
    names(4) = "Melons" 
    names(5) = "Bar" 

    For x = 0 To 2 
     results(x) = names(CInt(Int((5 * Rnd()) + 1))) 
    Next x 

    Return results 
End Function 



Function DisplayResults(ByRef UserTokens, ByVal UserAnswer, ByRef number1, ByRef number2, ByRef number3) 
    Console.WriteLine("*** " & number1 & " ** " & number2 & " ** " & number3 & " ***") 
    If UserTokens > 0 Then 
     Console.WriteLine("You have " & UserTokens & " tokens left, do you want to play again? (yes or no)") 
     UserAnswer = (Console.ReadLine()) 
    Else 
     Console.WriteLine("Sorry, you lost all of your tokens!") 
    End If 

    Return UserAnswer 
End Function 


End Module 
+0

ありがとうございました! –