本人对oop不是很了解,做了一个简单的题,发现个问题,不知道是因为oop的原因,还是random的原因,代码如下:
class Coin
{
static void Main()
{
string exit = "0";
while (exit != "1")
{
Coined c1 = new Coined();
Coined c2 = new Coined();
Coined c3 = new Coined();
Coined c4 = new Coined();
Coined c5 = new Coined(); c1.Flip();
c2.Flip();
c3.Flip();
c5.Flip();
c4.Flip(); Console.Write(c1.getFace() + "\t");
Console.Write(c2.getFace() + "\t");
Console.Write(c3.getFace() + "\t");
Console.Write(c4.getFace() + "\t");
Console.Write(c5.getFace() + "\t");
Console.WriteLine(); Console.Write(c1.strValue() + "\t");
Console.Write(c2.strValue() + "\t");
Console.Write(c3.strValue() + "\t");
Console.Write(c4.strValue() + "\t");
Console.Write(c5.strValue() + "\t");
Console.WriteLine(); int value = c1.Value() + c2.Value() + c3.Value();
Console.WriteLine(value);
Console.WriteLine(); exit = Console.ReadLine();
Console.WriteLine();
}
}
} class Coined
{
Random r = new Random(DateTime.Now.Millisecond);
private int face = 0;
private int[] value = new int[] { 10, 20, 50, 100 };
private int sValue = 0;
public void Flip()
{
face = r.Next(2);
} public int getFace()
{
return face;
} public string strValue()
{
string s = "";
s = face == 0 ? "Heads" : "Tails";
return s;
} public int Value()
{
if (face != 0)
{
int i = r.Next(3);
sValue = value[i];
}
return sValue;
}
}不知道为什么出来的结果都是5个都是一样的,找了好久也没有找到。希望大家指点一下,谢谢。还有Random r = new Random(DateTime.Now.Millisecond)和Random r = new Random()有什么不一样的地方?
class Coin
{
static void Main()
{
string exit = "0";
while (exit != "1")
{
Coined c1 = new Coined();
Coined c2 = new Coined();
Coined c3 = new Coined();
Coined c4 = new Coined();
Coined c5 = new Coined(); c1.Flip();
c2.Flip();
c3.Flip();
c5.Flip();
c4.Flip(); Console.Write(c1.getFace() + "\t");
Console.Write(c2.getFace() + "\t");
Console.Write(c3.getFace() + "\t");
Console.Write(c4.getFace() + "\t");
Console.Write(c5.getFace() + "\t");
Console.WriteLine(); Console.Write(c1.strValue() + "\t");
Console.Write(c2.strValue() + "\t");
Console.Write(c3.strValue() + "\t");
Console.Write(c4.strValue() + "\t");
Console.Write(c5.strValue() + "\t");
Console.WriteLine(); int value = c1.Value() + c2.Value() + c3.Value();
Console.WriteLine(value);
Console.WriteLine(); exit = Console.ReadLine();
Console.WriteLine();
}
}
} class Coined
{
Random r = new Random(DateTime.Now.Millisecond);
private int face = 0;
private int[] value = new int[] { 10, 20, 50, 100 };
private int sValue = 0;
public void Flip()
{
face = r.Next(2);
} public int getFace()
{
return face;
} public string strValue()
{
string s = "";
s = face == 0 ? "Heads" : "Tails";
return s;
} public int Value()
{
if (face != 0)
{
int i = r.Next(3);
sValue = value[i];
}
return sValue;
}
}不知道为什么出来的结果都是5个都是一样的,找了好久也没有找到。希望大家指点一下,谢谢。还有Random r = new Random(DateTime.Now.Millisecond)和Random r = new Random()有什么不一样的地方?
解决方案 »
- c#问题,帮我看看这种情况怎么办
- C# windows应用程序 窗体间传值 和 窗体自检怎么解决?麻烦大家解答
- 用VS运行一段C#程序总提示有错误,大家帮我看看这种错误是什么原因
- 关于在 .net 程序中启动另外一个程序的问题~
- 关于dataGridView的几个 基础的 但是恼火的问题 (限制输入字符&消息函数)
- 请问QQ可以象MSN那样的机器人,或者可以自己开发一个插件呢?
- 语法问题:关于for循环中使用break语句,请高手指教一下,谢谢
- 什么时候,一个类的方法,用static?????????????...........
- 关于TreeNode Clone()的一个问题
- 我的QuickStart在自己的机子上运行不了!!
- Where查询语句中的日期问题
- 移动局部透明label的闪烁问题
int a=Convert.ToInt32(DateTime.Now.Millisecond);
Random ran = new Random();
int RandKey = ran.Next(0, a);
Console.WriteLine(RandKey);