设置带有字符的if语句?

时间:2015-11-02 04:18:31

标签: string if-statement character statements

这是我的代码:

#include <stdio.h> 
#include <string.h>

int main() 
{

    char input[99];
    int i, i2, i3, n;

    while(1) 
    {

    i = 0;
    i2 = 1;
    i3 = 2;
    n = 0;

        printf("I can recognize if you are laughing or not, please type in a string. Type \"bye\" to quit: ");

        scanf("%s", input);

        printf("\n");

        if(strcmp(input, "bye") == 0) 
        {
            printf("Bye now!\n");
            break;
        }

        int length_input = strlen(input);
    char h = h;
    char a = a;
    char o = o;
    char x = !;  

    printf("Your input is: %s\n", input);

        for(; i < length_input; (i+2)) 
        {

            if(input[i] !== h) // is not laughing? or to say: if input[i] =/= h then not laughing.
            {
                printf("You are not laughing...\n");
                i2 = length_input;
                n = 1;
                i = length_input;
                i3 = length_input;
            }

        }

    for(; i2< length_input; (i2+2))
    {

        if(input[i2] !== a || o || x) // is not laughing? or to say: if input[i] =/= a, o, or ! then not laughing.
        {
            printf("You are not laughing...\n");
            i2 = length_input;
            n = 1;
            i3 = length_input;
        }

    }

    for(; i3 < length_input; (i3+3)) 
    {
        if(input[i3] == x) //is laughing? or to say: if input[i2] = ! then laughing
        {
            printf("You are laughing!\n");
            i3 = length_input;
            n = 1;
        }   

    }

    if(n == 0)
    {
        printf("You are not laughing...\n");
    }


        printf("\n");        
    }
}

正如标题所说,我认为我主要在if语句中遇到问题。特别是在输入[i]!== h的地方,输入[i]!== a || o || x等。任何帮助修复我的代码,以便功能非常感谢。

代码应该重现的例子是:

  

我能辨认你是不是在笑,请输入一个字符串:   公顷!

     

您的意见是:哈!

     

你在笑!

     

我能辨认你是不是在笑,请输入一个字符串:   哈哈!

     

您的意见是:哈哈!

     

你在笑!

     

我能辨认你是不是在笑,请输入一个字符串:   嗬!

     

您的输入是:ho!

     

你在笑!

     

我能辨认你是不是在笑,请输入一个字符串:   hohohaha!

     

您的意见是:hohohaha!

     

你在笑!

     

我能辨认你是不是在笑,请输入一个字符串:   HAA!

     

您的意见是:哈!

     

你不是在笑......

     

我能辨认你是不是在笑,请输入一个字符串:   公顷!!

     

您的输入是:ha !!

     

你不是在笑......

     

我能辨认你是不是在笑,请输入一个字符串:   公顷!公顷

     

您的意见是:哈!哈

     

你不是在笑......

     

我能辨认你是不是在笑,请输入一个字符串:   再见

     

再见!

1 个答案:

答案 0 :(得分:0)

由于这似乎是C,因此您需要使用!=代替!==。此外,如果您想查看变量x是否不等于变量y或变量z,则使用由&amp;&amp;分隔的两个不同的比较。示例:x!= y&amp;&amp; x!= z