我的 C 程序没有提供所需的输出
My C program is not giving desired output
我是 C 的初学者和新手。我正在使用 C 做一个数字猜谜游戏,但它没有正确执行,我的 if 语句没有在这段代码中执行,请帮忙。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int num, guess, count;
count = 1;
srand(time(0));
num = (rand()%10 + 1);
do
{
printf("Guess the number: \n");
scanf("%d", guess);
if(guess>num)
{
printf("too large");
}
else if(guess<num)
{
printf("too small");
}
else
{
printf("you won!\nIn %d count.", count);
}
count++;
}while(guess != num);
return 0;
}
代码应该给出输出
Guess the number
5
too small!
Guess the number
7
You won in 2 count
但是在scanf函数之后并没有执行if else语句和break循环。请帮忙
你的scanf不正确:
scanf("%d", guess); // should be scanf("%d", &guess);
你的if语句没有问题。问题是 scanf() 采用格式字符串和指针,但不是格式和非指针变量。
我是 C 的初学者和新手。我正在使用 C 做一个数字猜谜游戏,但它没有正确执行,我的 if 语句没有在这段代码中执行,请帮忙。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int num, guess, count;
count = 1;
srand(time(0));
num = (rand()%10 + 1);
do
{
printf("Guess the number: \n");
scanf("%d", guess);
if(guess>num)
{
printf("too large");
}
else if(guess<num)
{
printf("too small");
}
else
{
printf("you won!\nIn %d count.", count);
}
count++;
}while(guess != num);
return 0;
}
代码应该给出输出
Guess the number
5
too small!
Guess the number
7
You won in 2 count
但是在scanf函数之后并没有执行if else语句和break循环。请帮忙
你的scanf不正确:
scanf("%d", guess); // should be scanf("%d", &guess);
你的if语句没有问题。问题是 scanf() 采用格式字符串和指针,但不是格式和非指针变量。