2016-11-22 1 views
-3

그래서 Pokemon이 아무데도 나타나지 않으면 프로그램의 끝 부분에 도달하면 같은 Pokemon을 반복해서 반복합니다. 예 : 나는 내가 내가 발생할 안전하다 내가 다이 나는 Mr.Mime 발생 다이 Mr.Mime I 패배 내가 Mr.Mime 가 발생 안전하다 안전하다 Mr.Mime 패배 Mr.Mime 발생 Mr.MimeC# Console Application -

어떻게 중지하나요?

내 코드 :

using System; 
using System.Linq; 
using System.Threading; 

public class Program 
{ 
    public static void Main() 
    { 
     // Due to dotnetfiddle.net limitations I cannot store the pokémons data to a file 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("|   Hello there! Welcome to the world of pokémon! My name is Oak! People call me the pokémon Prof!   |");     
     Console.WriteLine("|This world is inhabited by creatures called pokémon! For some people, pokémon are pets. Others use them for fights.|"); 
     Console.WriteLine("|         Myself...I study pokémon as a profession.          |"); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("     |       Would you like to start (S)?      |"); 
     Console.WriteLine("     |                    |"); 
     Console.WriteLine("     -------------------------------------------------------------------------------"); 
     String fog = Console.ReadLine(); 
     String [] random = {"Pidgeot", "Jigglypuff", "Beedrill", "Caterpie", "Squirtle", "Charizard", "Charmander", "Bulbasaur", "Rattata", "Diglett", "Meowth", "Psyduck", "Dugtrio", "Magnemite", "Mr. Mime", "Gyarados", "Magikarp", "Onix", "Drowzee"}; 
     String [] gen = {"♂", "♀"}; 
     String [] name = {"Charmander", "Bulbasaur", "Squirtle"}; 
     if (fog == "s" || fog == "S" || fog == "start" || fog == "Start") 

     { 
      Random rnd = new Random(); 
      int HP = rnd.Next(20, 20); 
      int Atk = rnd.Next(20, 20); 
      int Def = rnd.Next(20, 20); 
      int Lvl = rnd.Next(5, 5); 
      int PN = rnd.Next(1, 721); 
      Console.WriteLine("You have chosen to generate a pokémon's!"); 
      Console.WriteLine(" Your pokémon's Name is: " + name[new Random().Next(0, name.Length)]); 
      Console.WriteLine(" Your pokémon's ㏋ is: " + HP); 
      Console.WriteLine(" Your pokémon's Attack is: " + Atk); 
      Console.WriteLine(" Your pokémon's Defense is: " + Def); 
      Console.WriteLine(" Your pokémon's Lvl is: " + Lvl); 
      Console.WriteLine(" Your pokémon's Gender is: " + gen[new Random().Next(0, gen.Length)]); 
      Console.WriteLine(" Your pokémon's Pokédex number is: " + PN); 

      Console.Write("Loading"); 
      for(int i = 0; i < 10; i++) 
      { 

      Console.Write("."); 
       Thread.Sleep(200); 
      } 
     Console.WriteLine(" "); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("|      You exit the proffesors lab to journey into the world of pokemon       |");     
     Console.WriteLine("|    Throughout your journey you may encounter wild pokemon that wish to fight and trainers    |"); 
     Console.WriteLine("|                             |"); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("So your journey begins... 30 paces to the gym type (s)"); // Loop 30 times with random chance of battleing 
      string lf = Console.ReadLine(); 
      if (lf == "s" || lf == "S" || lf == "start" || lf == "Start") 

      Enumerable.Repeat<Action>(() => 
      { 
       int Find = rnd.Next(1, 3); 
       if (Find == 1) 
       { 
       Console.WriteLine("You Encountered a pokémon"); 
      int HP2 = rnd.Next(1, 100); 
      int Atk2 = rnd.Next(1, 60); 
      int Def2 = rnd.Next(1, 40); 
      int Lvl2 = rnd.Next(1, 100); 
      int PN2 = rnd.Next(1, 721); 

      if (Atk >= Def2) 
      { 
       Console.WriteLine("You defeated " + random[new Random().Next(0, random.Length)]); 
      } 
      else 
      { 
      Console.WriteLine("Your pokémon died... Luckily a stranger appeared out of nowhere and revivded it for you so you can continue to battle"); 
      } 
       } 
       else 
       {  
      Console.WriteLine("You are safe this time"); 
       } 
      }, 30).ToList().ForEach(x => x()); 

     } 


     else 
     { 
     Console.WriteLine("Sorry to see you go so soon. I hope to meey you one day ~Oak"); 
     } 
    } 
} 

답변

0

문제는이 라인에있다.

Console.WriteLine("You defeated " + random[new Random().Next(0, random.Length)]); 

Random() 생성자가 시스템 시간을 기준으로하기 때문에 동일한 임의 값을 매번지고, 그것은 호출 사이에 충분한 변경되지 않습니다.

시계 가까이 연속 서로 다른 임의의 객체를 생성하기 위해 매개 변수가없는 생성자를 사용하여 유한 해상도를 가지고 있기 때문에 동일한 생산 난수 생성기를 만들고, 그러나 ...

아래에 인용 Random Class 설명서를 참조하십시오 난수 시퀀스. 다음의 예는 연속적으로 인스턴스화 된 두 개의 무작위 객체가 동일한 일련의 난수를 번 생성하는 방법을 보여줍니다. 대부분의 Windows 시스템에서 임의의 오브젝트는 15 밀리 초 내에 서로 작성되어 에 동일한 시드 값을 가질 가능성이 있습니다.

이 다음

Console.WriteLine("You defeated " + random[rnd.Next(0, random.Length)]); 
+0

이 도움말 ComradeJoecool –

+0

@Arron 데이비스 문제가 없습니다 주셔서 감사합니다 시도 해결하려면! – ComradeJoecool

관련 문제