编写代码但有一些错误

时间:2015-12-03 09:07:48

标签: 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语句,但是有一些逻辑或语法错误。我无法解决问题。感谢提前感谢任何帮助。

1 个答案:

答案 0 :(得分: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;
        }
    }