2015-11-11 53 views
0

我想制作一个程序,用户输入一个字符串并将该字符串存储在名为word1的列表中。我想从列表中选择一个随机字符串,并将其显示在标签中。我正在尝试使用多种方法和类作为练习。这里是我的代码:如何从列表中选择一个随机字符串并将其分配给一个变量C#

这是班上的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; 
    } 

这一个是在main.cs(非主)和它谈论到Class。就像我说的这部分可能是不必要的,但我试图用多种方法练习。

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; 
    } 

这一个是在Form1.cs和设置标签flashCards1的返回值:

private void go_Click(object sender, EventArgs e) 
    { 
     menu.Visible = false; 
     cards.Visible = true; 

     label1.Text = class1.flashCards1(); 

    } 

这也是Form1.cs和保存文本列表:

private void enter_Click(object sender, EventArgs e) 
    { 
     List<string> word1 = main.GetList1(); 

     word1.Add(textBox1.Text); 

     main.SetList1(word1); 
    } 

当我运行这段代码它给我的错误:

System.ArgumentOutOfRangeException was unhandled HResult=-2146233086 Message='maxValue' must be greater than zero. Parameter name: maxValue ParamName=maxValue Source=mscorlib StackTrace: at System.Random.Next(Int32 maxValue) at WindowsFormsApplication1.Class1.flashCards1() in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Class1.cs:line 19 at WindowsFormsApplication1.Form1.go_Click(Object sender, EventArgs e) in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 62 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at WindowsFormsApplication1.Program.Main() in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

我试图输入很多值,但它似乎不工作。我也看了这个帖子: How could I get a random string from a list and assign it to a variable

但是他们的解决方案给了我同样的错误。 任何帮助将不胜感激。对不起,如果这是太多/很少的信息。我试着说得通。

+1

您发布的代码甚至不会编译,更不用说显示运行时错误。你必须简化你的问题到[最小完整和可验证的例子](http://stackoverflow.com/help/mcve)并发布。此外,请包括完整的错误消息,包括堆栈跟踪。不要仅仅从错误信息中挑出几个字;每个字都意味着什么。例如“'maxValue'必须大于零”可能意味着你有一个名为'maxValue'的变量,在你没有向我们显示你没有初始化的地方。 –

+0

是的,我知道,我没有给出所有的代码,因为这是相当多的,因为我正在练习不同的课程。感谢您的反馈。 – minipanda1

+0

另外我没有看到复制到剪贴板选项的异常,所以我只是复制我认为我应该有的。我更新了帖子 – minipanda1

回答

5

您的问题与Random.Next(int)方法。当您尝试使用负值作为最大参数时,会出现此异常。这是因为它返回的值在>= 0 and < maxValue之间。它获取由负值搞砸了(-1你的情况,当列表中有0项)

解决方法是简单的,只是改变:

 int rtn = rnd.Next(word1.Count - 1); 

到:

 int rtn = rnd.Next(word1.Count); 

这应该因为随机总是返回一个值小于的最大值,所以应该从0到N-1访问,其中N是你列表的大小。

之后你会遇到更多的问题(与这个直接问题相关的问题更少),因为核心问题是你在flashcards1()函数中使用它之前没有初始化你的列表并添加了值。在向我们展示的示例中如何解决这个问题并不清楚,但我建议您查看一下函数的执行顺序。

+0

啊好的。谢谢。我会看看它运行的顺序:) – minipanda1

相关问题