- 分享
- 0
- 人气
- 0
- 主题
- 3
- 帖子
- 198
- UID
- 19026
- 积分
- 2953
- 阅读权限
- 20
- 注册时间
- 2005-10-14
- 最后登录
- 2018-9-19
- 在线时间
- 1609 小时
|
参考以下的代码:em0026
Original:
#include <iostream> // 没用到
#include <cstdlib>
#include <cmath> // 没用到
#include <ctime>
using namespace std; // 没用到
int main ()
{
int guess;
char answ; // 没用到
srand (time (0));
for (int i = 0; i < 100; i++) // 只能 loop 100 次吗?
{
int random = rand(); // 不应该放在 loop 里面
int num = ((random % 100) + 1);
printf("Guess a number between 1 to 100:\n");
scanf("%d", guess); 应该用 &guess
if (guess > num)
{
printf("the number you guess is too smaller. Try again??\n");
continue;
}
if (guess < num)
{
printf("the number you guess is too bigger. Try again??\n");
continue;
}
else if (guess = num) // 应该是 guess == num
{
printf("congrulation !!");
break;
}
}
return 0;
}
我的:
- #include <cstdio>
- #include <cstdlib>
- #include <ctime>
- int main(void)
- {
- int iRandom, iGuess;
- srand(time(0));
- iRandom = rand() % 100 + 1;
- do {
- printf("Guess a number between 1 to 100:\nGuess: ");
- scanf("%d", &iGuess);
-
- if (iGuess > iRandom)
- {
- printf("\nThe number you entered is too large! Try again?\n");
- }
- else if(iGuess < iRandom)
- {
- printf("\nThe number you entered is too small! Try again?\n");
- }
- else
- {
- printf("Congratulation!\n");
- }
- } while (iRandom != iGuess);
- return 0;
- }
复制代码
[ 本帖最后由 Dhilip89 于 2009-11-10 05:36 PM 编辑 ] |
|