Visual C++ 基礎篇 rand() 隨機值

#include <iostream>
#include <ctime> //必須加入
using namespace std;
void main()
{
int i,a;
srand((unsigned) time(0));
a = (rand() 0);
cout << a;
//printf("%d",a);
//------------------------------
//for (i = 0; i < 5; ++i)
//{
//printf("%d \n", rand());
//}
//------------------------------
//for (i = 0; i < 1; ++i)
//{
//printf("%d \n", rand());
//}
system("Pause");
}
範例一
#include <iostream>
#include <ctime>
using namespace std;
void main()
{
srand((unsigned)time(0));
int w = (rand() % 100), x, i;
//cout << w;
for (i = 1; i <= i + 1; i++) {
cout << "來猜我的體重,猜第" << i << "次" << endl; //第幾次執行迴圈
cin >> x;

if (x < w)
{
cout << "我有這麼輕嗎?" << endl;
continue; //強迫程式直接回到迴圈開頭
}
else if (x > w)
{
cout << "我有這麼重嗎?" << endl;
continue; //強迫程式直接回到迴圈開頭
}
else
{
cout << "您答對了\n";
break; // 跳離For 迴圈
}
}
system("Pause");
}
已邀請:

要回復主題請先登入註冊