2014-03-05 1 views
0

C#으로 주사위 시뮬레이터 프로그램을 만들었지 만 할당 요구 사항에서 사용자가 원하는만큼 반복 할 수 있어야한다고합니다.반복 프로그램 C#

그것은 파이썬에서 간단 :

def var(): 

    import random 

    dicesides = int(input("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: ")) 
    script(dicesides, random) 

def script(dicesides, random): 
    if dicesides in (4,6,12): 
     dice = int(random.randrange(1, dicesides)) 
     print(dicesides, " sided dice, score", dice) 
    else: 
     print("That number is invalid. Please try again.") 
     var() 

    repeat = str(input("Repeat? Simply put yes or no : ")) 

    if repeat == "yes": 
     var() 
    else: 
     quit()    
var() 

그리고 이것은 내 C# 코드 :

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

      int num; 
      Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "); 
      num = Convert.ToInt32(Console.ReadLine()); 

      if (num == 4 || num == 6 || num == 12) 
      { 
       int randomDice = random.Next(0, num); 
       Console.WriteLine(num + " sided dice thrown, " + randomDice); 
      } 
      else 
      { 
       Console.WriteLine("That number is invalid. Please try again.") 
      } 


     } 

가 어떻게 프로그램이 반복 얻을 것?

+3

분명히 루프가 필요합니다. –

+1

그런데'random.Next()'를 호출하면 하한으로'1', 상한으로'num + 1'을 사용해야합니다 , 0에서 5로가는 D6을 시뮬레이트하지 않는 한 – StriplingWarrior

+0

@DStanley : 출구 조건이 충족 될 때까지 프로세스를 반복하는 것과 같은 일을 여전히 수행합니다. 어떻게 끝났는 지 상관없이 루프입니다. 필자와의 유사점은 매우 분명하며 한 줄의 파이썬 코드를 작성한 적이 없다. "반복해서 ..."또는 "이 조건이 존재하는 동안"은 매우 분명합니다. –

답변

4

while(true)break A를 사용 분리 함수에서 루프의 용기를 넣어 호출

Random random = new Random(); 
    while(true) 
    { 
     int num; 
     Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "); 
     num = Convert.ToInt32(Console.ReadLine()); 

     if (num == 4 || num == 6 || num == 12) 
     { 
      int randomDice = random.Next(0, num); 
      Console.WriteLine(num + " sided dice thrown, " + randomDice); 
     } 
     else 
     { 
      Console.WriteLine("That number is invalid. Please try again.") 
     } 
     Console.WriteLine("Try again? (yes/no)"); 
     if(Console.ReadLine().ToLower() != "yes") 
      break; // end the while loop 
    } 

참고 클리너 방법 것이라고 그 루프에서 :

static Random random = new Random(); // make the Random a field of the class for reuse 
public static void Main() 
    while(true) 
    { 
     RollDie(); 
     Console.WriteLine("Try again? (yes/no)"); 
     if(Console.ReadLine().ToLower() != "yes") 
      break; // end the while loop 
    } 
} 
public static void RollDie() 
{ 
    int num; 
    while(true) 
     Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "); 
     num = Convert.ToInt32(Console.ReadLine()); 

     if (num == 4 || num == 6 || num == 12) 
     { 
      int randomDice = random.Next(0, num); 
      Console.WriteLine(num + " sided dice thrown, " + randomDice); 
      break; // exit the while loop 
     } 
     else 
     { 
      Console.WriteLine("That number is invalid. Please try again.") 
     } 
    } 
} 
1

다음과 같이 코드를 while 루프에 넣어야합니다.

static void Main(string[] args) 
    { 
     Console.WriteLine("Do you want to roll the dice ? Y/N"); 
     char c = Convert.ToChar(Console.ReadLine()); 
     while(c.ToLower() == 'y') 
     { 
     Random random = new Random(); 

     int num; 
     Console.WriteLine("Please enter the amount of sides you want the dice that is being thrown to have. The dice sides available are: 4, 6 and 12: "); 
     num = Convert.ToInt32(Console.ReadLine()); 

     if (num == 4 || num == 6 || num == 12) 
     { 
      int randomDice = random.Next(0, num); 
      Console.WriteLine(num + " sided dice thrown, " + randomDice); 
     } 
     else 
     { 
      Console.WriteLine("That number is invalid. Please try again.") 
     } 
     Console.WriteLine("Do you want to roll it again? Y/N"); 
     c = Convert.ToChar(Console.ReadLine()); 
     } 

    } 

희망 ...