2016-06-18 4 views
-5

나는 당신이 그것을 내가 쓴 6000 번 랜드() 함수는

을 던질 때 주사위의 각면의 수를 표시하는 프로그램을 구축하고자 랜드() 함수에 문제가 제대로 작동하지 않습니다 코드

#include <iostream> 
using namespace std; 
#include <iomanip> 
using std::setw; 
#include <cstdlib> 
#include <ctime> 
int main() 
{ 
int face ; 
int frequency1 =0; 
int frequency2 =0; 
int frequency3 =0; 
int frequency4 =0; 
int frequency5 =0; 
int frequency6 =0; 
for(int counter =1;counter <=6000;counter++){ 

     face = 1+rand()%6; 
     switch(face){ 
    case 1: 
     ++frequency1; 
     break; 

    case 2: 
     ++frequency1; 
     break; 

    case 3: 
     ++frequency1; 
     break; 

    case 4: 
     ++frequency1; 
     break; 

    case 5: 
     ++frequency1; 
     break; 

    case 6: 
     ++frequency1; 
     break; 
    default: 
     cout<<"program should never get here!!! "; 
     break; 
    }} 
    cout<<"the number of face 1 is : "<<frequency1<<endl; 
cout<<"the number of face 2 is : "<<frequency2<<endl; 
cout<<"the number of face 3 is : "<<frequency3<<endl; 
cout<<"the number of face 4 is : "<<frequency4<<endl; 
cout<<"the number of face 5 is : " <<frequency5<<endl; 
cout<<"the number of face 6 is : "  <<frequency6<<endl; 
return 0; 
} 

내가 그것을

the number of face 1 is : 6000 
the number of face 2 is : 0 
the number of face 3 is : 0 
the number of face 4 is : 0 
the number of face 5 is : 0 
the number of face 6 is : 0 
+7

항상 'frequency1'에 추가하고 있습니다. 무엇을 기대합니까? – Li357

+2

또한 깃발거에게는 반드시 중복되는 것은 아닙니다. 인쇄 상 오류가 있습니다. – Li357

+0

@AndrewL 아, 당신은 절대적으로 맞습니다. [단 한 손으로 그런 질문을 끝내기 위해서는 이러한 힘이 있어야합니다.] (http://meta.stackoverflow.com/questions/326250/would-it- m3 % c3 % b6lnir-close-voting-for-lang의 확장 기능을 유용하게 사용할 수 있습니다. –

답변

0

당신은 단지 대신 다른 경우 frequency1에 추가 계속 같은 일을 보여줍니다이 코드를 실행할 때마다. 나는 너에게 설명하려고 애썼지 만 너는 여전히 이해하지 못하는 것 같다. 여기에 편집 된 코드 :

#include <iostream> 
using namespace std; 
#include <iomanip> 
using std::setw; 
#include <cstdlib> 
#include <ctime> 

int main() { 
    int face; 
    int frequency1 = 0; 
    int frequency2 = 0; 
    int frequency3 = 0; 
    int frequency4 = 0; 
    int frequency5 = 0; 
    int frequency6 = 0; 
    for(int counter =1;counter <=6000;counter++){ 
     face = 1+rand()%6; 
     switch(face){ 
      case 1: 
       ++frequency1; 
       break; 

      case 2: 
       ++frequency2; 
       break; 

      case 3: 
       ++frequency3; 
       break; 

      case 4: 
       ++frequency4; 
       break; 

      case 5: 
       ++frequency5; 
       break; 

      case 6: 
       ++frequency6; 
       break; 
      default: 
       cout<<"program should never get here!!! "; 
       break; 
     } 
    } 
    cout<<"the number of face 1 is : "<<frequency1<<endl; 
    cout<<"the number of face 2 is : "<<frequency2<<endl; 
    cout<<"the number of face 3 is : "<<frequency3<<endl; 
    cout<<"the number of face 4 is : "<<frequency4<<endl; 
    cout<<"the number of face 5 is : "<<frequency5<<endl; 
    cout<<"the number of face 6 is : "<<frequency6<<endl; 
    return 0; 
} 

보시다시피

, 원래 코드 않는 모든 경우 문에서 ++frequency1 (증가한다). 이 편집 된 코드가 작동해야합니다. 나는 이미 이것을 설명했다.

관련 문제