試圖製作隨機生成X值在高值和低值之間的隨機數的代碼。代碼也應該防止生成相同的隨機數。RNG未知錯誤
這裏是我的代碼至今:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Randonnumbers
{
class Program
{
static void Main(string[] args)
{
int lowrange = 0; // Establishing the variables for lowest number, highest number, and total numbers generated.
int highrange = 50;
int numberof = 10;
var generated = new List<int>(); // Creates a list of numbers that have been generated already to prevent double-ups.
var rng = new Random();
Console.WriteLine("Numbers Generated:");
while (numberof > 0) //Repeats this loop once for each "numberof". (until the number hits zero)
{
generatenewnumber(); //Calls the function above
}
Console.ReadLine(); // Here so the program doesnt instantly close
}
public void generatenewnumber()
{
int number = rng.Next(lowrange, highrange); //Sets the number to a random number between the high and low values.
if (!generated.Contains("number")) // Checks if the rg'd number has already been made.
{
generatenewnumber(); //If it has, it will re-run this function.
}
else
{
numberof = numberof - 1;
generated.Add(number); // Adds the genereated number to the list.
Console.WriteLine(number); // Prints the number.
}
}
}
}
不知道哪裏出錯。 程序不會運行,也不會顯示任何錯誤。
也許它與從一個條件內自己調用函數有關?說實話也沒辦法,我是很新的C#.C#
oops,我離開了「」在該行...(固定刪除「的」,因爲'數字'是生成的數字變量。然而,代碼仍然沒有運行...任何其他的想法:頁 –