问题描述
您好,
i我正在学习c#并且现在创建了一个简单的项目,只需创建6个
随机数。
我的表单包含一个按钮和6个随机数标签。该程序
似乎工作正常,
然而,当点击几个按钮后程序崩溃。
有谁知道在哪里原因可能是?
-
亲切的问候
Thomas Neubauer
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Text;
使用System.Windows.Forms;
名称空间WindowsApplication1
{
公共部分课程表格1:表格
{
public Form1()
{
InitializeComponent();
}
private void btZieheLottozahlen_Click(object sender,EventArgs e)
{
int [] Zahlen = new int [6];
Random Zufallszahl = new Random();
int Doppelte = 0;
do
{
for(int i = 0; i< 6; i ++)
{
Zahlen [i] = Zufallszahl.Next(1,64);
}
Array.Sort(Zahlen);
for(int i = 0;我< 5; i ++)
if(Zahlen [i] == Zahlen [i + 1])
Doppelte = 1;
} while(Doppelte == 1);
label1.Text = Convert.ToString(Zahlen [0]);
label2.Text = Convert.ToString( Zahlen [1]);
label3.Text = Convert.ToString(Zahlen [2]);
label4.Text = Convert.ToString(Zahlen [3]);
label5.Text = Convert.ToString(Zahlen [4]);
label6.Text = Convert.ToString(Zahlen [5]);
}
}
}
Hello,
i am learning c# and have created now a simple project that just creates 6
random numbers.
My form includes a button and 6 labels for the random numbers. The program
seems to work correct,
however when after a few button clicks the program crashes.
Does anyone know where the reason could be?
--
Kind regards
Thomas Neubauer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btZieheLottozahlen_Click(object sender, EventArgs e)
{
int[] Zahlen = new int[6];
Random Zufallszahl = new Random();
int Doppelte = 0;
do
{
for (int i = 0; i < 6; i++)
{
Zahlen[i] = Zufallszahl.Next(1, 64);
}
Array.Sort(Zahlen);
for (int i = 0; i < 5; i++)
if (Zahlen[i] == Zahlen[i + 1])
Doppelte = 1;
} while (Doppelte == 1);
label1.Text = Convert.ToString(Zahlen[0]);
label2.Text = Convert.ToString(Zahlen[1]);
label3.Text = Convert.ToString(Zahlen[2]);
label4.Text = Convert.ToString(Zahlen[3]);
label5.Text = Convert.ToString(Zahlen[4]);
label6.Text = Convert.ToString(Zahlen[5]);
}
}
}
推荐答案
好吧,它以什么方式崩溃?有没有例外?
-
Jon Skeet - < sk *** @ pobox.com>
博客:
世界级。英国的NET培训:
Well, in what way does it crash? Is there an exception?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
我发布的代码中没有任何明显的东西会导致崩溃。
哪个不是说代码没有错误:
*我不会发送一个int来做一个bool'的工作,但那个问题
不应该导致程序崩溃。
*也许更令人沮丧的是,你永远不会重置Doppelte。在你的
循环中,所以如果你没有一系列独特的随机数,
你的循环将永远不会退出。
后一个问题会导致你的程序停止响应,但是a)
我不会称之为崩溃,而b)如果你打断中的程序
你可以很容易地看到程序卡在哪里的调试器,你还可以轻松地通过代码看看会发生什么变化
变量并且希望当一个人没有像你一样行事时注意
期望它。
调试器可以帮助你。你应该使用它。 :)
尽管如此,你的帖子中还没有真正具体。如果
以上的信息没有帮助,那么崩溃是什么?意思?你有没有获得例外的
?如果是这样,有什么例外?在什么行的
代码会发生?您的申请还包含哪些其他代码?
Pete
I don''t see anything obvious in the code you posted that would cause a crash.
Which is not to say the code is bug-free:
* I wouldn''t send an int in to do a bool''s job, but that problem
shouldn''t cause your program to crash.
* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.
The latter problem would cause your program to stop responding, but a)
I wouldn''t call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn''t behaving as you
expect it to.
The debugger is there to help you. You should use it. :)
Still, you haven''t really been very specific in your post. If the
above information doesn''t help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?
Pete
这篇关于几次执行后程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!