【问题标题】:Wrote a code but there are some errors写了一个代码,但有一些错误
【发布时间】: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 &amp;&amp; choice == 0 'Y') ??????这应该是if(check==0 &amp;&amp; choice == 'Y')
  • 我无法对其进行测试,但只是查看代码我发现您在 checkNumber 函数代码周围缺少大括号,并且缺少函数 checkNumber(int n, int g)
  • checkNumber() 定义的函数没有参数,甚至没有使用括号。
  • 另外,还有if (n&lt;g),后跟else if (n&lt;g)
  • void checkNumber() 返回类型为 void,而您返回的是整数值。

标签: c if-statement random void


【解决方案1】:

语法很糟糕,甚至无法编译。 我认为这应该可行:

#include <stdio.h>
#include <stdlib.h>

int randomNumber();
int checkNumber(int,int);
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 == 'Y')
        {
            printf("***********\nDo you want to continue to guess number?(Y/N)\n");
            scanf("%c",&choice);
            printf("***********\n");

        }
        else break;
    }
    return 0;

}
    int randomNumber(){

        return (rand()%10);
    }

    int checkNumber(int n, int g){

        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;
        }
    }

【讨论】:

  • 您没有修复前向声明:checkNumber 用两个 float 参数声明,而它实际上需要两个 ints,而 randomNumberint 声明参数,而实际函数不接受任何参数。
  • 是的,我已经修复了你提到的一些内容,其余部分刚刚修复:)
  • 你忘记在randomNumber()的return语句中添加;
猜你喜欢
  • 2017-04-07
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多