c - 写了一段代码,但是有一些错误

标签 c if-statement random void

#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 语句,但存在一些逻辑或语法错误。我无法解决该问题。感谢任何帮助,提前致谢。

最佳答案

语法非常糟糕,甚至无法编译。 我认为这应该有效:

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

关于c - 写了一段代码,但是有一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34061787/

相关文章:

c - 二进制常量前的keil uvision 5语法错误

c++ - strdup() 和 strcpy

PHP 查询 + IF ELSE

c - 在 linux c 中使用按键打破循环

c++ - C++ 中 "if error then fail fast"的性能损失?

if-statement - 如果左侧的单元格为空,是否可以使用 arrayformula 复制上面的值?

python - 'random' 的独立实例

c++ - 如何使用 SDL 游戏在 C++ 中获取随机值

java - 生成包含字母数字和特殊字符的随机字符串?

c - 如果字符为 null,则打印 null