using System;
using System.Linq;
namespace Loto
{
class Program
{
static void Main(string[] args)
{
short[] tirage = new short[6]; //array that contains the numbers
short nbTirage; //the number randomed
for (int i = 0; i < 6; i++)
{
nbTirage = NombreAleatoire();
while (tirage.Contains(nbTirage)) //if the number has already been drawn
{
nbTirage = NombreAleatoire(); //we random another one
}
Console.Write(nbTirage + " "); //writing the result
}
}
static short NombreAleatoire()
{
Random nb = new Random();
return (short)nb.Next(1, 49);
}
}
}
這是完整的程序。C#程序只能在跨越模式下正常工作
它應該畫出1到49之間包含7個唯一的數字。該程序在調試模式下運行良好,但是當我從exe中運行它時,它會繪製相同數字的7倍。是什麼原因造成的?我正在使用Visual Studio。
你嘗試重建該程序? – Needham
您需要保留一個Random對象,並在一個對象上調用Next .. +我沒有看到填充「tirage」+您可能爲了簡潔而刪除名稱空間和類。 –
祝您嘗試製作LOTTO程序win ..否則,我們都會在一段時間之前變得富有。再加上使用調試器來遍歷你的代碼並檢查你正在創建的對象,你會立即看到缺陷發生的地方。 – MethodMan