jeudi 23 novembre 2017

Why it doesn't generate random swords name and price each time? [duplicate]

This question already has an answer here:

I want each Sword to have random name and price but they all get the same random name and price if i run the program without debug from Visual Studio.

enter image description here

I tried running the code line by line in Visual Studio and it actually worked fine.

enter image description here

Can someone explain me why this is happening, I am very confused...

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;
    }
}

}




Aucun commentaire:

Enregistrer un commentaire