2014-09-02 1 views
0

저는 C#에 익숙하지 않습니다. 같은 문제가있는 다른 게시물을 검색 한 후 많은 도움을받을 수 없었습니다. 디버깅 할 때마다 문구가 다르지만 매번 다른 문구가 아닌 동일한 문구가 반복됩니다.프레이즈 생성기가 같은 문구를 계속 반복합니다.

using System; 

public class Program 
{ 

    static String[] nouns = new String[3] { "He", "She", "It" }; 
    static String[] adjectives = new String[5] { "loudly", "quickly", "poorly", "greatly", "wisely" }; 
    static String[] verbs = new String[5] { "climbed", "danced", "cried", "flew", "died" }; 
    static Random rnd = new Random(); 
    static int noun = rnd.Next(0, nouns.Length); 
    static int adjective = rnd.Next(0, adjectives.Length); 
    static int verb = rnd.Next(0, verbs.Length); 

    static void Main() 
    { 
     for (int rep = 0; rep < 5; rep++) 
     { 
      Console.WriteLine("{0} {1} {2}", nouns[noun], adjectives[adjective], verbs[verb]); 
     } 
    } 
} 

답변

2

정적 변수는 프로그램이 처음로드 될 때 한 번만 초기화됩니다. 당신이 생성

static void Main() 
{ 
    for (int rep = 0; rep < 5; rep++) 
    { 
     int noun = rnd.Next(0, nouns.Length); 
     int adjective = rnd.Next(0, adjectives.Length); 
     int verb = rnd.Next(0, verbs.Length); 
     Console.WriteLine("{0} {1} {2}", nouns[noun], adjectives[adjective], verbs[verb]); 
    } 
} 

이 방법을 : 그래서 당신은 다음과 같이 루프 내부로 이동한다 -

당신은 nounadjective, 그리고 verb는 (재) 일 새 문구를 인쇄 할 때마다 생성 할 필요가 루프를 실행할 때마다 새로운 무작위 값.

관련 문제