2017-11-24 3 views
0

Ich möchte, dass jedes Schwert einen zufälligen Namen und einen zufälligen Preis hat, aber alle erhalten denselben Namen und denselben Preis, wenn ich das Programm ohne Debug von Visual Studio starte.Warum generiert es nicht jedes Mal zufällige Schwerter Name und Preis?

enter image description here

Ich habe versucht, den Code Zeile für Zeile in Visual Studio ausgeführt wird und es funktionierte wirklich gut.

enter image description here

Kann mir jemand erklären, warum dies geschieht, ich bin sehr verwirrt ...

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

Sie benötigen nur einen RNG für die gesamte App, nicht für jede Methode und sicherlich nicht jedes Mal, wenn Sie sie verwenden. In der Dokumentation ausführlich beschrieben – Plutonix

+0

Für die nächste Zeit, bitte posten Sie eine [MCVE] anstelle aller dieser nicht verwandten Code. Außerdem gibt es viel bessere Möglichkeiten als 'if i == 1, sonst wenn 1 == 2 ....' –

Antwort

1

auf den Code der Suche und den Kommentar zu Ihnen, die Random speichern Objekt, das Sie erstellen, sobald Ihre Anwendung gestartet wird. Wenn Sie dann einen zufälligen Wert benötigen, rufen Sie .Next() auf, um Ihre neue Zufallszahl zurückzugeben.

+0

Nein, es gibt keine Notwendigkeit, die NET Random-Klasse zu "säen". Es benutzt bereits einen ziemlich willkürlichen Samen. Das Problem ist nicht auf die Saat – Plutonix

+0

Ich nahm an und so habe ich meine Antwort bearbeitet, nachdem Sie Ihren Kommentar gesehen. Vielen Dank @Plutonix. –

+0

@Plutonix Als ich das letzte Mal den Code durchgesehen habe, habe ich festgestellt, dass "Environment.TickCount" verwendet wurde. Das könnte sich geändert haben, aber – john

Verwandte Themen