這個是第二個程式碼
因為我覺得還滿簡單的,所以我就沒寫註解了
另外,你的題目有點看不懂,所以我寫的可能跟你要的不一樣
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0)); //產生亂數種子
int y;
y=rand()%100;
int min=0,max=100,guess;
int time=0;
char try_again;
do
{
cout << "*************************" << endl;
cout << " c++ 猜數字遊戲 " << endl;
cout << "*************************" << endl;
cout << "\n";
cout << "輸入數字 : ";
cin >> guess;
if(guess == y)
{
cout << "一次就答對了 !!" << endl;
cout << "Try again ?(y/n) ";
cin >> try_again;
}
else
{
cout << "再輸入一次" << endl;
int i=0;
for(i=0;i>=0;i++)
{
cout << "輸入數字 : ";
cin >> guess;
if(guess == y)
break;
else
{
if(guess > y)
{
cout << "小一點" << endl;
if(guess < max)
max=guess;
}
else if(guess < y)
{
cout << "大一點" << endl;
if(guess > min)
min=guess;
}
cout << "介於" << min << "和" << max << "之間" << endl;
}
}
cout << "你總共輸入" << i+2 << "次" << endl;
cout << "Try again ?(y/n)";
cin >> try_again;
}
}while(try_again == 'Y' || try_again == 'y');
system("pause");
return 0;
}