如果条件不起作用,在以下c程序中

时间:2015-07-16 08:02:21

标签: c if-statement char

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int iRandom;
    int iResponse;
    srand(time(0));

    iRandom = (rand() % 10) + 1;

    printf("\nGuess a number between 1 and 10: ");
    scanf("%c", &iResponse);

    if (isdigit(iResponse)) {

        iResponse = iResponse - '0';

        if (iResponse >=1 && iResponse <=10 ) {


            if (iResponse == iRandom) {
                printf("\nYou guessed right.\n");
            }
            else {
                printf("\nSorry, you guessed wrong.\n");
                printf("The correct guess was %d\n", iRandom);
            }
        }
        else {
            printf("You did not enter a digit between 1 and 10.");
        }

    }
    else {
        printf("\nYou did not enter a digit.\n");
    }

    getch();
    return 0;
}

如果(iResponse&gt; = 1&amp;&amp; iResponse&lt; = 10)不起作用,请检查此情况。 在这种情况下,如果iResponse&lt; = 10为假,那么它将不会执行else部分。 任何解决方案都可以帮助我。

6 个答案:

答案 0 :(得分:1)

我认为你的意思是以下

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

int main( void )
{
    char line[20];
    unsigned int iRandom;
    unsigned int iResponse;

    const unsigned int Base = 10;

    srand( ( unsigned int )time( NULL ) );

    iRandom = rand() % Base + 1;

    printf( "\nGuess a number between 1 and %u: ", Base );
    scanf( "%20s", line );

    errno = 0;
    iResponse = ( unsigned int )strtoul( line, NULL, Base );

    if ( !errno && iResponse >= 1 && iResponse <=10 ) 
    {
        if ( iResponse == iRandom ) 
        {
            puts( "\nYou guessed right." );
        }
        else 
        {
            puts( "\nSorry, you guessed wrong." );
            printf( "The correct guess was %u\n", iRandom );
        }
    }
    else 
    {
        puts( "\nYou did not enter a number" );
    }

    return 0;
}

程序输出可能看起来如下

Guess a number between 1 and 10: A
You did not enter a number

Guess a number between 1 and 10: 4
Sorry, you guessed wrong.
The correct guess was 3

Guess a number between 1 and 10: 4
You guessed right.

至于你的代码,至少在本声明中

scanf("%c", &iResponse);

您应该将"%c"替换为" %c"(%c之前的空格)。即使在这种情况下,结果也将是实现定义的。而且使用这种方法,您将无法输入可接受的数字10。:)

答案 1 :(得分:0)

问题出在这一行

 scanf("%c", &iResponse);

当您输入例如47作为输入时,它只需要4而不是47它只读取一个字符,即&#39; 4&#39; 。 &#39; 47&#39;不是一个角色。因此,每当您输入一些数字时,它将始终进入该条件。但是,如果你试图进入&#39;作为输入它将返回你

You did not enter a digit

您可以使用以下代码

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int iRandom;
    int iResponse;
    srand(time(0));

    iRandom = (rand() % 10) + 1;

    printf("\nGuess a number between 1 and 10: ");
    scanf("%d", &iResponse);

    if (isdigit(iResponse+'0') || iResponse==10) {



        if (iResponse >=1 && iResponse <=10 ) {


            if (iResponse == iRandom) {
                printf("\nYou guessed right.\n");
            }
            else {
                printf("\nSorry, you guessed wrong.\n");
                printf("The correct guess was %d\n", iRandom);
            }
        }
        else {
            printf("You did not enter a digit between 1 and 10.");
        }

    }
    else {
        printf("\nYou did not enter a digit.\n");
    }

    getch();
    return 0;
}

答案 2 :(得分:0)

更改

scanf("%c", &iResponse);

if (isdigit(iResponse)) {
    . . .

if (scanf("%d", &iResponse)) {
    . . .

工作代码看起来像

#include <stdio.h>
#include <time.h>

int main() {
    srand(time(0));
    int iRandom = (rand() % 10) + 1, iResponse;
    printf("\nGuess a number between 1 and 10: ");
    if (scanf("%d", &iResponse)) {
        if (iResponse >= 1 && iResponse <= 10 ) {
            if (iResponse == iRandom)
                printf("\nYou guessed right.\n");
            else 
                printf("\nSorry, The correct guess was %d\n", iRandom);
        }
        else
            printf("You did not enter a number between 1 and 10.");
    }
    else
        printf("\nYou did not enter a number.\n");
    return 0;
}

<强>输入

1

示例输出

Sorry, The correct guess was 9

查看演示http://ideone.com/V7GIeK

答案 3 :(得分:0)

实际上您将iResponse声明为integer变量,而在scanf中您将说明符%c写为字符。

scanf("%c", &iResponse);

只需更改为 -

scanf("%d", &iResponse);

答案 4 :(得分:0)

为了对你的程序有一个好的方法,我认为你想检查用户输入,以确保它是你期望的那个。 这意味着您需要检查输入是否仅包含数字。 试试这个:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define RANDOM 10

void clean_stdin(void){
    int c;
    while((c = getchar()) != 0 && c != '\n');
}

int inputCheck(void){
    int number;
    char c;

    do{
        printf("\tPlease type a Number: ");
        if (scanf("%d%c", &number, &c) == 0 || c != '\n'){
            printf("\nLetter[s] was Found in your Answear\n\n\n");
            clean_stdin();
        }else{
            break;
        }
    }while(TRUE);

    return number;
}

int main(void){
    int iResponse;
    int iRandom ;
    srand ( (unsigned int)time(NULL) );

    iRandom = (rand() % 10 + 1);
    iResponse = inputCheck();

    if(iRandom == iResponse){
        printf("\nYou guessed right.\n");
    }else{
        printf( "\nSorry, you guessed wrong.\n" );
        printf( "\n\t\t\tThe correct guess was %u\n", iRandom );
    }

    return(0);
}

你会得到:

  Please type a Number: 3r
Letter[s] was Found in your Answear


    Please type a Number: G7

Letter[s] was Found in your Answear


    Please type a Number: 6

Sorry, you guessed wrong.

            The correct guess was 4
  Please type a Number: 4
You guessed right.

通过这种方式,您可以告知用户他/她的输入。

如果您想通知用户该号码在1到10之间,您会喜欢这样:

    #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define MIN_RANDOM 1
#define Max_RANDOM 10

void clean_stdin(void){
    int c;
    while((c = getchar()) != 0 && c != '\n');
}

int inputCheck(int min, int max){
    int number;
    char c;

    do{
    printf("\tPlease type a Number::\t");
    if (scanf("%d%c", &number, &c) == 0 || c != '\n'){
        clean_stdin();
        printf("\nLetter[s] was Found in your Answear\n\n\n");
    }else if(number < min || number > max){
        printf("The number has to be wetween %d and %d\n\n",min, max);
    }else{
        break;
    }
    }while(TRUE);

    return number;
}

int main(void){
    int iResponse;
    int iRandom ;
    srand ( (unsigned int)time(NULL) );

    iRandom = (rand() % Max_RANDOM + MIN_RANDOM);
    iResponse = inputCheck(MIN_RANDOM,Max_RANDOM);

    if(iRandom == iResponse){
    printf("\nYou guessed right.\n");
    }else{
    printf( "\nSorry, you guessed wrong.\n" );
    printf( "\n\t\t\tThe correct guess was %u\n", iRandom );
    }

    return(0);
}

输出将是这样的:

  Please type a Number::  A1
Letter[s] was Found in your Answear


    Please type a Number::  1A

Letter[s] was Found in your Answear


    Please type a Number::  15
The number has to be wetween 1 and 10

    Please type a Number::  4

Sorry, you guessed wrong.

            The correct guess was 8

答案 5 :(得分:-2)

尝试制作

char iResponse;

看到这个有效

enter image description here

enter image description here