#include <iostream> #include <cstdlib> //引入才能夠使用rand()函數 #include <ctime> //引入才能夠使用time()函數 using namespace std; int main() { double num; cout << "請問骰子要丟幾次? "; cin >> num; //等待使用者輸入一整數 cout << endl; //換行 srand(time(NULL)); //設定亂數種子,需要#include <ctime> double one=0,two=0,three=0,four=0,five=0,six=0; //各點數數目變數 for(int n=1;n<=num;n++) { int dicenum=rand()%6+1; //將骰子點數指定給dicenum變數 if(dicenum==1) //如果dicenum等於1 { one++; } else if(dicenum==2) { two++; } else if(dicenum==3) { three++; } else if(dicenum==4) { four++; } else if(dicenum==5) { five++; } else if(dicenum==6) { six++; } } cout << "以下是丟骰子結果統計:" << endl << "1點:" << one << " 百分比:" << 100*one/num << "%" << endl //輸出一點的數目,並且計算佔總數的百分比 << "2點:" << two << " 百分比:" << 100*two/num << "%" << endl << "3點:" << three << " 百分比:" << 100*three/num << "%" << endl << "4點:" << four << " 百分比:" << 100*four/num << "%" << endl << "5點:" << five << " 百分比:" << 100*five/num << "%" << endl << "6點:" << six << " 百分比:" << 100*six/num << "%" << endl; return 0; }