2015-11-02 79 views
0

這裏是我的代碼:設置帶有字符的if語句?

#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!

您的輸入是:haa!

你是不是笑...

,如果你在笑我可以識別或不能,請輸入一個字符串: 哈!

您的輸入是:ha!

你是不是笑...

如果你或笑我不能夠識別,請輸入一個字符串: 哈哈

你輸入:哈哈

你是不是笑...

如果你或笑我不能夠識別,請輸入一個字符串: 再見

再見!

回答

0

由於這看起來是C,所以您會想用!=代替!==。此外,如果您想查看變量x是否不等於變量y或變量z,則使用兩個不同的比較,分別用& &分隔。例如:x!= y & & x!= z

+0

如何將x存儲爲'!'?我也有一個錯誤。 –

+0

你把它放在單引號中嗎?從我在你的代碼中看到的,你正在使用'char x =!',它與'char x ='!''不一樣。事實上,我看到你對a和o做同樣的事情。 –