C# 루프가 이상해요 반복이 2번씩 되네요

C# 루프가 이상해요 반복이 2번씩 되네요

작성일 2009.10.20댓글 1건
    게시물 수정 , 삭제는 로그인 필요

클래스 3개로 주사위 게임을 만드는데...

 

여기 case 10같은경우 반복이 이상하게 되는데 제가 뭔가 잘못한게 있나요?

Your dice roll totalled {0}

Press any key to roll dice again to obtain your {0} points

 하고 한번씩 딱딱 끊겨야되는데 안멈추고 그냥 2번씩 반복을 해버려요. 거기다 다른y/n 같은것도 스킵해버릴때가 있구요. 뭐가 잘못된건가요?

 

 

public void Play() {

do{

int a = die1.Roll();

int b = die2.Roll();

int sum = a+b;

//declare int sum and roll dice

die1.Roll();

die2.Roll();

Console.WriteLine("\n Your dice roll totalled {0}", sum);

switch(sum)

......

...

...

...

case 10:

//declare second sum

int sum2 = 0;

do{

Console.WriteLine("Press any key to roll dice again to obtain your {0} points.", sum);

Console.Read();

//// show the first sum

sum2 = die1.Roll()+ die2.Roll();

// calculate second sum

Console.WriteLine("Your dice roll totalled" + sum2);

// show the second sum

if (sum2 == 7)

{

Console.WriteLine("\n You lose - I got two points", sum2);

cPoint = cPoint + 2;// add computer's points

}

else if (sum2 == sum)

{

Console.WriteLine("You've obtained {0} points", sum);

mPoint = mPoint + sum;// add my points

}

} while ((sum2 != 7)&&(sum!=sum2)) ;// if sum2 is same with sum or sum2 is 7 end while statement

//end do..while

break;



profile_image 익명 작성일 -

코드 전문을 보지않아서 확신할수는 없지만.. 
Console.Read()라는 메서드가 의도하지 않는 문제를 발생시키곤 하더군요..
물론 이 메서드 자체에 문제가 있는것은 아니구요..

Console.Read()의 MSDN의 설명을 보면 
"사용자의 입력에 줄바꿈문자열을 추가시킨다"고 되어있습니다.
플로우가 돌다가 Console.Read()를 만나면 대기하죠.
여기서 예를들어 사용자가 엔터키를 누르면 줄바꿈 문자인 '\r\n'이 입력되는데
여기에 '\r\n'이 한번 더 추가되는거에요..
증상을 보니 얼핏 이 문제인듯한데..

코드 중간에 Console.Read()을
사용자의 입력을 기다리려는 목적만으로 사용하신거라면
Console.ReadLine()이나 Console.ReadKey()같은 메서드로 교체해보세요..