【发布时间】:2020-11-01 06:27:15
【问题描述】:
我正在尝试制作一个简单的石头剪刀布游戏。我需要计算机从 ROCK、PAPER 和 SCISSORS 数组中随机选择一个字符串。这是我目前所拥有的:
public string GetComputerChoice()
{
string computerChoice = null;
string[] computerChoices = { "ROCK", "PAPER", "SCISSORS" };
return computerChoice[Random.Next(computerChoices.Length)];
}
我在 Visual Studio 中遇到的唯一错误是“下一个”,它显示“随机不包含“下一个”的定义。
我对整个编程完全陌生。关于为什么这不起作用或我可以做些什么来使它起作用的任何提示? 我已经阅读了类似帖子的其他回复,但似乎所有答案都只是代码块,无法解释其工作原理。
【问题讨论】:
-
我怀疑
System.Random与UnityEngine.Random的命名空间冲突 ...您可以使用Random.Range(0, computerChoices.Length) -
这也回答了你的问题:stackoverflow.com/questions/2019417/…?
标签: c# arrays string unity3d random