我正在尝试创建一个用户输入字符串的程序,然后将该字符串存储在名为word1的List中.我想从List中选择一个随机字符串,并将其显示在标签中.我正在尝试使用多种方法和类作为练习.这是我的代码:
这是在Class1.cs类中:
main main = new main();
public string flashCards1()
{
List<string> word1 = main.GetList1();
Random rnd = new Random();
int rtn = rnd.Next(word1.Count - 1);
string word = word1[rtn];
string rtnWord = word.ToString();
return rtnWord;
}
Run Code Online (Sandbox Code Playgroud)
这个是在main.cs(不是Main)中,它与Class1对话.就像我说这部分可能是不必要的,但我试图用多种方法练习.
private List<string> word2 = new List<string>();
private List<string> word1 = new List<string>();
public List<string> GetList1()
{
return word1;
}
public void SetList1(List<string> updatedList)
{
word1 = updatedList;
}
Run Code Online (Sandbox Code Playgroud)
这个是在Form1.cs中,并将标签设置为flashCards1的返回值:
private void go_Click(object sender, EventArgs e)
{
menu.Visible = false;
cards.Visible = true;
label1.Text = class1.flashCards1();
}
Run Code Online (Sandbox Code Playgroud)
这也是在Form1.cs中并将文本保存到List:
private void enter_Click(object sender, EventArgs e)
{
List<string> word1 = main.GetList1();
word1.Add(textBox1.Text);
main.SetList1(word1);
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,它给出了我的错误:
System.ArgumentOutOfRangeException未处理HResult = -2146233086 Message ='maxValue'必须大于零.参数名称:包括maxValue = PARAMNAME源包括maxValue = mscorlib程序堆栈跟踪:在System.Random.Next(的Int32包括maxValue)在WindowsFormsApplication1.Class1.flashCards1()在C:\用户\ lottes \源\工作区\工作区\ WindowsFormsApplication1\WindowsFormsApplication1\Class1的. CS:在WindowsFormsApplication1.Form1.go_Click(对象发件人,EventArgs e)在C线19:\ Users \用户lottes \源\工作区\工作区\ WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs中:线62在System.Windows.Forms.Control的. OnClick(EventArgs e)在System.Windows.Forms.OnClick(EventArgs e)的System.Windows.Forms.OnMouseUp(MouseEventArgs mevent)处于System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,在System.Windows上的System.Windows.Forms.Button.WndProc(Message&m)的System.Windows.Forms.ButtonBase.WndProc(Message&m)处的System.Windows.Forms.Control.WndProc(Message&m)处进行Int32单击). System.Windows.Forms.Control.ControlNativeWindow中的Forms.Control.ControlNativeWindow.OnMessage(Message&m).WndProc(消息&m)上System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&MSG)在System.Windows.Forms的. System.Windows.Forms上的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)中的Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData) .Application.ThreadContext.RunMessageLoop(的Int32原因,ApplicationContext的上下文)在System.Windows.Forms.Application.Run(表格的MainForm)在WindowsFormsApplication1.Program.Main()在C:\用户\ lottes \源\工作区\工作区\ WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:System.AppDomain.ExecuteAssembly的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)第19行(String assemblyFile,Evidence assemblySecurity,String [] args)a 吨Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context在System.Threading.ExecutionContext.RunInternal(对象状态)(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)在的System.Threading. ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象状态)在System.Threading.ThreadHelper.ThreadStart()的InnerException:
我试图输入很多值,但它似乎不起作用.我也查看了这个帖子: 我如何从列表中获取随机字符串并将其分配给变量
然而他们的解决方案给了我同样的错误.任何帮助将不胜感激.对不起,如果这是太多/很少的信息.我试着理解.
你的问题是使用Random.Next(int)方法.当您尝试使用负值作为最大参数时,会出现此异常.这是因为它返回一个值>= 0 and < maxValue.哪个被负值弄乱了(在你的情况下,当列表有0个项时为-1)
修复很简单,只需更改:
int rtn = rnd.Next(word1.Count - 1);
Run Code Online (Sandbox Code Playgroud)
成:
int rtn = rnd.Next(word1.Count);
Run Code Online (Sandbox Code Playgroud)
这应该有效,因为random总是返回一个小于最大值的值,所以应该访问0到N - 1,其中N是列表的大小.
在那个问题之后你会遇到更多的问题(并且与这个直接问题的关联性较小)但是因为核心问题是你没有初始化你的列表并在flashcards1()函数中使用它之前添加了值.目前尚不清楚如何在您向我们展示的示例中解决这个问题,但我建议您查看函数执行的顺序.