如何将string()转换为字符串

时间:2018-08-14 07:28:22

标签: arrays string vb.net

嗨,我正在用Visual Basic(作为控制台应用程序)制作老虎机游戏,我似乎无法将从数组中随机选择的值放入字符串数据类型。它告诉我

  

string()的值不能转换为字符串

这是我的代码:

slot1 = {"Apple", "Cherry", "Banana"}
slot2 = {"Apple", "Cherry", "Banana"}
slot3 = {"Apple", "Cherry", "Banana"}

Console.WriteLine(slot1(rand.Next(0, slot1.Length)))
slotmachine1 = slot1

If slotmachine1 = "Apple" Then
    Console.ForegroundColor = ConsoleColor.Green
ElseIf slotmachine1 = "Cherry" Then
    Console.ForegroundColor = ConsoleColor.Red
ElseIf slotmachine1 = "Banana" Then
    Console.ForegroundColor = ConsoleColor.Yellow
End If

否则,该程序可以正常运行,因此有人知道此修复程序吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您正在尝试将一个数组分配给一个字符串。将您的随机数保存到变量中,然后使用该变量从数组中读取值。

Dim slotmachine1 as String
dim RandomNumber as integer

slot1 = {"Apple", "Cherry", "Banana"}
slot2 = {"Apple", "Cherry", "Banana"}
slot3 = {"Apple", "Cherry", "Banana"}

RandomNumber = rand.Next(0, slot1.Length)

Console.WriteLine(slot1(RandomNumber))
slotmachine1 = slot1(RandomNumber)

If slotmachine1 = "Apple" Then
    Console.ForegroundColor = ConsoleColor.Green
ElseIf slotmachine1 = "Cherry" Then
    Console.ForegroundColor = ConsoleColor.Red
ElseIf slotmachine1 = "Banana" Then
    Console.ForegroundColor = ConsoleColor.Yellow
End If