- 註冊時間
- 2006-7-11
- 最後登錄
- 2022-3-11
- 主題
- 查看
- 積分
- 180
- 閱讀權限
- 40
- 文章
- 253
- 相冊
- 0
- 日誌
- 0
![Rank: 4](static/image/common/star_level3.gif)
狀態︰
離線
|
從網路下載一猜數字遊戲
因剛學c語言
所以想把它改寫後卻執行有問題
想請教大大那裡出錯
謝謝
原c++程式碼
#include <iostream>
#include <stdio.h>
using namespace std;
int main( int argc, char *argv[] )
{
printf( "\n HsK test! \n\n 猜數字遊戲 \n" );
int byby = 5;
srand((unsigned) time(NULL)); //剛更新 ^^
int min = 0, max = 100, round = 0, this_round = rand()%100+1;
int player;
std::string again;
while(byby == 5)
{
cout << " Max=" << max << " ; Min=" << min << " ;" << endl;
cout << " I Guess :";
cin >> player;
cout << endl;
if (player <= min || player >= max)
{
cout << "注意!!!" << endl;
if (player <= min)
player = min;
else if (player >= max)
player = max;
}
else
{
if (player == this_round)
{
if (round == 0)
{
cout << "真厲害!! 一次就猜中!" << endl;
cout << "另起新局? (Y/N)" << endl;
cout << " ------>";
cin >> again;
if(again=="Y"||again=="y")
{
min = 0;
max = 100;
round = 0;
this_round = rand () % 100 + 1;
}
else
break;
}
else
{
printf( "\n..bingo..猜數字遊戲 %d次猜中 \n", round);
cout << "另起新局? (Y/N)" << endl;
cout << " ------>";
cin >> again;
if (again == "Y" || again == "y")
{
min = 0;
max = 100;
round = 0;
this_round = rand () % 100 + 1;
}
else
break;
}
}
else
{
if (player > this_round)
{
max = player;
}
else
{
min = player;
}
round += 1;
}
}
}
return 1;
}
改寫成c後如下
請大大們幫小弟改正好嗎
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main( int argc, char *argv [] )
{
char again;
int byby = 5;
srand((unsigned) time(NULL)); //剛更新 ^^
int min = 0, max = 99, round = 0, this_round = rand()%100+0;
int player;
while(byby == 5)
{ printf(" max=%d; min=%d;\n",max,min);
printf("I Guess :");
scanf("%d",&player);
printf("\n");
if (player <= min || player >= max)
{
printf("注意!!!\n") ;
if (player <= min)
player = min;
else if (player >= max)
player = max;
}
else
{
if (player == this_round)
{
if (round == 0)
{
printf("真厲害!! 一次就猜中!");
printf("另起新局? (Y/N)" );
printf(" ------>");
again=getche();
if(again=='Y'||again=='y')
{
min = 0;
max = 99;
round = 0;
this_round = rand () % 100 + 0;
}
else
break;
}
else
{
printf( "\n..bingo.. 猜數字遊戲 %d次猜中 \n", round);
printf("另起新局? (Y/N)");
printf(" ------>");
scanf("%c",&again);
if (again == 'Y' || again == 'y')
{
min = 0;
max = 99;
round = 0;
this_round = rand () % 100 + 0;
}
else
break;
}
}
else
{
if (player > this_round)
{
max = player;
printf("數目猜小一點\n");
}
else
{
min = player;
printf("數目猜大一點\n");
}
round += 1;
}
}
}
system("pause");
return 0;
}
《 本帖最後由 csr 於 2011-4-18 22:07 編輯 》 |
|