【发布时间】:2015-12-03 09:07:48
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int randomNumber(int);
void checkNumber(float,float);
int main()
{
int number ,guess = 0,check=0;
char choice='Y';
number = randomNumber();
printf("%d\n",number);
while(1)
{
switch(choice)
{
case 'Y':
printf("Guess the number that is between 1-20:\n");
scanf("%d",&guess);
checkNumber(number,guess);
break;
case 'N'
printf("BYE BYE....\n")
break;
default:
printf("Please enter a valid choice\n");
}
if(check==0 && choice == 0 'Y')
{
printf("***********\nDo you want to contiune to guess number?(Y/N)\n");
scanf("%c",&choice);
printf("***********\n");
}
else break;
}
return 0;
int randomNumber()
{
return (rand()%10)
}
}
void checkNumber()
if (n<g)
{
printf("Guess a lower value!\n\n");
return 0;
}
else if (n<g)
{
printf("Guess a higher value!\n\n");
return 0;
}
else
{
printf("****Cong.,You guessed the number ****\n\n");
return 1;
}
让用户猜测程序选择的幸运数字的程序。它使用一个 for 循环和大量 if 语句,但存在一些逻辑或语法错误。我无法解决问题。感谢任何帮助,提前致谢。
【问题讨论】:
-
if(check==0 && choice == 0 'Y')??????这应该是if(check==0 && choice == 'Y') -
我无法对其进行测试,但只是查看代码我发现您在 checkNumber 函数代码周围缺少大括号,并且缺少函数 checkNumber(int n, int g)
-
checkNumber()定义的函数没有参数,甚至没有使用括号。 -
另外,还有
if (n<g),后跟else if (n<g) -
void checkNumber()返回类型为 void,而您返回的是整数值。
标签: c if-statement random void