C++ rand()函数不使用';t工作正常
我对rand()函数有问题 我想建立一个程序,当你掷6000次骰子时,显示每一面骰子的数量 我写了这段代码C++ rand()函数不使用';t工作正常,c++,C++,我对rand()函数有问题 我想建立一个程序,当你掷6000次骰子时,显示每一面骰子的数量 我写了这段代码 #include <iostream> using namespace std; #include <iomanip> using std::setw; #include <cstdlib> #include <ctime> int main() { int face ; int frequency1 =0; int frequency2 =
#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;
}
您只需不断添加到
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;
}
#包括
使用名称空间std;
#包括
使用std::setw;
#包括
#包括
int main(){
内面;
int frequency1=0;
int frequency2=0;
int frequency3=0;
int frequency4=0;
int frequency5=0;
int frequency6=0;
对于(int counter=1;您总是添加到frequency1
的计数器),您期望得到什么?另外,对于标记器来说,这不一定是重复的。只是一个印刷品error@AndrewL啊,你完全正确,也许使用数组也会使生活更简单。@πάνταῥεῖ 完全同意。像这样的问题应该结束
#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;
}