C# 为什么没有';每次都不会随机生成剑名和价格?
我希望每把剑都有随机的名称和价格,但如果我在没有从VisualStudio调试的情况下运行程序,它们都会得到相同的随机名称和价格 我试着在VisualStudio中一行一行地运行代码,实际上效果很好 有人能解释一下为什么会这样,我很困惑C# 为什么没有';每次都不会随机生成剑名和价格?,c#,random,console-application,C#,Random,Console Application,我希望每把剑都有随机的名称和价格,但如果我在没有从VisualStudio调试的情况下运行程序,它们都会得到相同的随机名称和价格 我试着在VisualStudio中一行一行地运行代码,实际上效果很好 有人能解释一下为什么会这样,我很困惑 using System; namespace HomeA { class Swords { public string name; public int price; public bool availableToBuy; }
using System;
namespace HomeA
{
class Swords
{
public string name;
public int price;
public bool availableToBuy;
}
class MainClass
{
public static void Main ()
{
bool exitShop = false;
// Name Input and Gold Reward
string userName = MyReadString ("Hello Stranger! What's your Name?");
int userGold = new Random ().Next (40, 120);;
Console.Clear ();
// Generate Random Array of Swords
Swords[] arrayOfSwords = GenerateRandomSwords();
do{ // Shop Loop
// Welcome Message and Choose Input!
Console.WriteLine (new String('-', 29 + userName.Length) + "\n Welcome " + userName + " to the Sword Shop!" +
"\n You have " + userGold + " Gold!\n" + new String('-', 29 + userName.Length) +
"\n1. Buy a Sword!\n2. Sell a Sword(Coming Soon)!\n3. Display my Swords(Coming Soon)!\n0. Continue your Adventure!\n\n");
int menuInput = MyReadInt ("What would you like to do?", 0, 3);
Console.WriteLine (menuInput);
if(menuInput == 0) // Exit
{
exitShop = true;
Console.Clear();
}else if(menuInput == 1) // Buy
{
Console.Clear();
Console.WriteLine (new String('-', 37) +
"\n You have " + userGold + " Gold available to spend!\n" +
new String('-', 37));
// Display all Available Swords
for(int i = 0; i <= arrayOfSwords.Length -1; i++)
{
if(arrayOfSwords[i].availableToBuy)
{
Console.WriteLine("{0}. Swords of {1} is available for {2} Gold!", (i+1), arrayOfSwords[i].name, arrayOfSwords[i].price);
}
}
// 1 Additional Option to go Back to the Shop
Console.WriteLine("{0}. Go Back!", arrayOfSwords.Length + 1);
// Get Input which Sword to Buy
bool loopAgain = false;
do
{ // Check if it's Available to Buy
int userBuyMenuInput = MyReadInt("\n\nWhich Sword would you like to Buy?", 1, arrayOfSwords.Length + 1);
if(userBuyMenuInput == arrayOfSwords.Length + 1)
{ // Exit to Shop Page
Console.Clear();
break;
}
if(arrayOfSwords[userBuyMenuInput - 1].availableToBuy == false)
{
loopAgain = true;
Console.WriteLine("There is no Sword with number {0}!", userBuyMenuInput);
}else
{
Console.Clear();
if(userGold >= arrayOfSwords[userBuyMenuInput - 1].price)
{ // Buy, deduct the gold and Output a message
arrayOfSwords[userBuyMenuInput - 1].availableToBuy = false;
userGold -= arrayOfSwords[userBuyMenuInput - 1].price;
Console.WriteLine("You Successfully Bought the Sword of {0}!", arrayOfSwords[userBuyMenuInput - 1].name);
loopAgain = false;
}else
{
Console.WriteLine("You Don't have enought Gold to buy the Sword of {0}!", arrayOfSwords[userBuyMenuInput - 1].name);
loopAgain = false;
}
}
}while(loopAgain);
}else if (menuInput == 2) // Sell
{
Console.Clear();
}else // Display
{
Console.Clear();
}
}while(!exitShop);
}
public static string MyReadString(string messageToDisplay)
{
string result;
do // Making sure input is not null
{
Console.WriteLine(messageToDisplay);
result = Console.ReadLine();
}while(result == null);
return result;
}
public static int MyReadInt(string messageToDisplay, int minAllowed, int maxAllowed)
{
int result;
do // Making sure input is within min and max Allowed
{
result = int.Parse(MyReadString(messageToDisplay));
if(result < minAllowed || result > maxAllowed) Console.WriteLine("Invalid Input! You must give a number between {0} and {1}!", minAllowed, maxAllowed);
}while(result < minAllowed || result > maxAllowed);
return result;
}
public static Swords[] GenerateRandomSwords()
{
// Create an Array of Swords of Random Length
Swords[] result = new Swords[new Random().Next (3, 9)];
// Populate each Instance of Swords with random values and make it available to buy
for (int i = 0; i <= result.Length - 1; i++)
{
result [i] = new Swords ();
result [i].name = GenerateRandomStringFromInt();
result [i].price = new Random ().Next (10, 30);
result [i].availableToBuy = true;
}
return result;
}
public static string GenerateRandomStringFromInt()
{
string result = "";
int loopXAmountOfTimes = new Random().Next(3, 8), loopCount = 0, randomInt;
do
{
// Add a char accouring to a random int
randomInt = new Random ().Next (1, 26);
if(randomInt == 1)
{
result += 'A';
}else if(randomInt == 2)
{
result += 'B';
}else if(randomInt == 3)
{
result += 'C';
}else if(randomInt == 4)
{
result += 'D';
}else if(randomInt == 5)
{
result += 'E';
}else if(randomInt == 6)
{
result += 'F';
}else if(randomInt == 7)
{
result += 'G';
}else if(randomInt == 8)
{
result += 'H';
}else if(randomInt == 9)
{
result += 'I';
}else if(randomInt == 10)
{
result += 'J';
}else if(randomInt == 11)
{
result += 'K';
}else if(randomInt == 12)
{
result += 'L';
}else if(randomInt == 13)
{
result += 'M';
}else if(randomInt == 14)
{
result += 'N';
}else if(randomInt == 15)
{
result += 'O';
}else if(randomInt == 16)
{
result += 'P';
}else if(randomInt == 17)
{
result += 'Q';
}else if(randomInt == 18)
{
result += 'R';
}else if(randomInt == 19)
{
result += 'S';
}else if(randomInt == 20)
{
result += 'T';
}else if(randomInt == 21)
{
result += 'U';
}else if(randomInt == 22)
{
result += 'V';
}else if(randomInt == 23)
{
result += 'W';
}else if(randomInt == 24)
{
result += 'X';
}else if(randomInt == 25)
{
result += 'Y';
}else
{
result += 'Z';
}
loopCount++;
}while(loopCount <= loopXAmountOfTimes);
return result;
}
}
使用系统;
命名空间HomeA
{
阶级之剑
{
公共字符串名称;
公共价格;
公共图书馆可供购买;
}
类主类
{
公共静态void Main()
{
bool exitShop=false;
//姓名输入和黄金奖励
string userName=MyReadString(“你好,陌生人!你叫什么名字?”);
int userGold=newrandom().Next(40120);;
Console.Clear();
//生成随机的剑数组
刀剑[]数组单词=生成器刀剑();
做{///购物循环
//欢迎留言并选择输入!
Console.WriteLine(新字符串('-',29+userName.Length)+“\n欢迎“+userName+”来到剑店!”+
“\n您有”+userGold+“Gold!\n”+新字符串('-',29+userName.Length)+
“\n1.买一把剑!\n2.卖一把剑(马上就来)!\n3.展示我的剑(马上就来)!\n0.继续你的冒险!\n\n”);
int menuInput=MyReadInt(“您想做什么?”,0,3);
Console.WriteLine(menuInput);
if(menuInput==0)//退出
{
exitShop=true;
Console.Clear();
}else如果(menuInput==1)//购买
{
Console.Clear();
Console.WriteLine(新字符串('-',37)+
\n您有“+userGold+”黄金可供消费!\n+
新字符串('-',37));
//显示所有可用的剑
对于(int i=0;i查看代码和注释,保存应用程序启动后创建的随机对象。
然后,当需要一个随机值时,调用.Next()在它上面返回你的新随机数。整个应用程序只需要一个RNG,而不是每个方法,当然也不是每次使用它时都需要。下一次请在文档中详细介绍,请发布一个,而不是所有不相关的代码。此外,如果i==1,还有比使用更好的方法,否则如果1==2….
不,没有需要“播种”NET Random类。它已经使用了一个相当任意的种子。问题与我假设的种子思想无关,因此我在看到您的评论后编辑了我的答案。非常感谢,@puropoix@puropoix上次我查看代码时,发现它使用了Environment.TickCount
。这可能已经改变了,however@john…你的意思是?@Protonix我在你的评论中加了一句。如果我以后看到你的名字,我会记下不要这样做的。