2014-09-16 5 views
0

사용자가 텍스트 문서에 20 개의 이름을 입력하도록 요청하는 프로그램을 만들었습니다. 파일이 존재하지 않으면 새로운 파일을 작성한 다음 내용을 표시하십시오.텍스트 문서에서 임의의 문자열 선택

무작위로 이름을 선택하기 위해 랜더 라이저를 사용하고 있습니다. 이미 여기에서 해봤지만 제대로 작동하지 않습니다. 텍스트 파일을 읽고 무작위로 이름을 선택하겠습니다. 오류가 없으며 내가 잘못하고 있는지 확실하지 않습니다.

참고 : 내가 한 것은 성을 선택하는 가능성이 커진 이름 (배열의 classNames [0])을 입력하게 한 것입니다. 여기

static void increaseChances() 
{ 
    int rand = r.Next(3); //0 = 100%, 1 = 50%, 2 = 33.33% chance, 3 = 25% chance, This number determines the percentage of the first name entered to be picked 

    if (rand == 0) 
    { 
     Console.ForegroundColor = ConsoleColor.White; 
     Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[0]); 
    } 
    else 
    { 
     Console.ForegroundColor = ConsoleColor.Yellow; 
     Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[r.Next(classNames.Length)]); 
     Console.ForegroundColor = ConsoleColor.White; 
    } 
} 

내가있어 무엇 : 파일이 이미 존재하는 경우에 당신은 당신의 classNames 배열을 작성하지 않는

class Program 
{ 
    static Random r = new Random(); 
    static string[] classNames = new string[20]; 

    static void increaseChances() 
    { 
     int rand = r.Next(3); //0 = 100%, 1 = 50%, 2 = 33.33% chance, 3 = 25% chance, This number determines the percentage of the first name entered to be picked 

     if (rand == 0) 
     { 
      Console.ForegroundColor = ConsoleColor.White; 
      Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[0]); 
     } 
     else 
     { 
      Console.ForegroundColor = ConsoleColor.Yellow; 
      Console.WriteLine("\nThe winner of the randomiser is: {0} Congratulations! ", classNames[r.Next(classNames.Length)]); 
      Console.ForegroundColor = ConsoleColor.White; 
     } 
    } 

    static void Main(string[] args) 
    { 
     Random RandString = new Random(); 

     string file = @"C:\names.txt"; 
     Console.ForegroundColor = ConsoleColor.White; 

     if (File.Exists(file)) 
     { 
      Console.WriteLine("Names in the text document are: \n"); 
      foreach (string displayFile in File.ReadAllLines(file)) 
      Console.WriteLine(displayFile); 
      increaseChances(); 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.Write("\nPress any key to close... "); 
      Console.ReadKey(); 
     } 
     else 
     { 
      for (int i = 0; i < 20; i++) 
      { 
       Console.Write("Enter name number {0}: ", i + 1); 
       classNames[i] = Console.ReadLine(); 
       File.Create(file).Close(); 
       File.WriteAllLines(file, classNames); 
      } 

       Console.WriteLine("Writing names to file..."); 
       increaseChances(); 
       Thread.Sleep(3000); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write("Completed! Exiting..."); 
       Thread.Sleep(1500); 
     } 
    } 
} 
+0

문제가 해결되지 않습니까? 무작위가 실제로 무작위가 아니라는 것을 알고 있습니까? – Sayse

+0

잊어 버린 중요한 세부 사항은 다음과 같습니다. 오류는 발생하지 않지만 어떤 일이 일어나지 않으려 고하고 있습니까? 작동하지 않는 것은 무엇입니까? –

+0

내가 원하는 것은 텍스트 파일을 읽고 그것을 임의의 이름으로 선택하여 표시하는 것입니다. – whiskybrah

답변

0

. 따라서 increaseChances 메소드에서 이름을 선택할 수 없습니다.

+0

그래, 고정 내가 해냈어 : StreamReader sr = new StreamReader (file); int i = 0; while (i whiskybrah

관련 문제