我希望每个Sword都有随机名称和价格,但如果我在没有Visual Studio调试的情况下运行程序,它们都会得到相同的随机名称和价格。
我尝试在Visual Studio中逐行运行代码,它实际上工作正常。
有人能解释我为什么会这样,我很困惑......
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;
}
}
}
答案 0 :(得分:1)
查看代码和注释,在应用程序启动后保存您创建的Random对象。 然后当你需要一个随机值时,在它上面调用.Next()来返回你的新随机数。