2013-10-14 73 views
-1

如果我为x输入值205,该函数应该打印出“first quad”,但是当我测试我的函数时,它会打印出“first quad”和“not sure”。我无法弄清楚为什么,任何帮助表示赞赏!为什么我的函数打印这条语句?

void checkRoom(int x) { 
    if ((x >= 203) || (x <= 216)) { 
     printf("first quad\n"); 
    } 

    if ((x >=217) || (x <= 229)) { 
     printf("second quad\n"); 
    } 

    if ((x >=232) || (x <= 238)) { 
     printf("CSL\n"); 
    } 

    if ((x >= 246) || (x <= 257)) { 
     printf("classroom wing\n"); 
    } 

    else { 
     printf("not sure\n"); 
    } 
} 
+2

我打电话给shenanigans。你的代码应该打印出除“'不确定\ n”'之外的所有消息。要么你没有准确地描述问题,要么你没有提供真实的代码。 –

+0

我很抱歉,但这绝对是我真正的代码,我永远不会发布无助或无效的内容。问题是当我为x输入205时,我的程序打印出“first quad”和“not sure”消息。 – Karen

+0

除非你的编译器被破坏了,否则你发布的代码绝对不会做你声称它在205'被传递给它时所做的。任何人都可以编译它并轻松演示它没有。如果你不相信我,请参考(http://ideone.com/HztlKJ)。 –

回答

2

因为else势必只有最后if,你需要的是else if。你的测试条件应该使用&&而不是||

void checkRoom(int x) { 
    if ((x >= 203) && (x <= 216)) { 
     printf("first quad\n"); 
    } 

    else if ((x >=217) && (x <= 229)) { 
     printf("second quad\n"); 
    } 

    else if ((x >=232) && (x <= 238)) { 
     printf("CSL\n"); 
    } 

    else if ((x >= 246) && (x <= 257)) { 
     printf("classroom wing\n"); 
    } 

    else { 
     printf("not sure\n"); 
    } 
} 
+0

如果我们不能使用“else if”会怎么样?有没有办法让它在没有这些的情况下完成? – Karen

+0

@凯伦你**不能**使用'else if'?这没有意义。 –

+0

是的,我的导师说我们不应该使用其他语句。 :(但我会再次问他,以防万一 – Karen

1
  1. 你的布尔表达式是不正确的。范围包含需要&&,而不是||

  2. else子句只适用于最新if声明;如果你想运行后面的if语句,只有在先前没有成功,那么你需要把它们放在它们自己的else条款中。

0

如果你绝对不能使用“否则,如果”再试试这三种之一:

  1. 设置一个标志,当条件满足,并在年底测试的条件。

    void checkRoom(int x) { 
        int printed = 0; 
        if ((x >= 203) && (x <= 216)) { 
         puts("first quad"); 
         printed = 1; 
        } 
        if ((x >= 217) && (x <= 229)) { 
         puts("second quad"); 
         printed = 1; 
        } 
        if ((x >=232) && (x <= 238)) { 
         puts("CSL"); 
         printed = 1; 
        } 
        if ((x >= 246) && (x <= 257)) { 
         puts("classroom wing"); 
         printed = 1; 
        } 
        if (printed == 0) { 
         puts("not sure"); 
        } 
    } 
    
  2. 打印后返回,所以你不会继续下去的决策树。

    void checkRoomOption2(int x) { 
        if ((x >= 203) && (x <= 216)) { 
         puts("first quad"); 
         return; 
        } 
        if ((x >= 217) && (x <= 229)) { 
         puts("second quad"); 
         return; 
        } 
        if ((x >=232) && (x <= 238)) { 
         puts("CSL"); 
         return; 
        } 
        if ((x >= 246) && (x <= 257)) { 
         puts("classroom wing"); 
         return; 
        } 
        puts("not sure"); 
    } 
    
  3. 设定的返回值“不知道”,然后覆盖它时,它匹配

    void checkRoomOption3(int x) { 
        // you can only do this with a pointer if all the strings are literal strings 
        // because then they have static storage and no memory needs to be allocated 
        const char *r = "not sure"; 
        if ((x >= 203) && (x <= 216)) { 
         r = "first quad"; 
        } 
        if ((x >= 217) && (x <= 229)) { 
         r = "second quad"; 
        } 
        if ((x >=232) && (x <= 238)) { 
         r = "CSL"; 
        } 
        if ((x >= 246) && (x <= 257)) { 
         r = "classroom wing"; 
        } 
        puts(r); 
    } 
    
0

如果你不想使用“否则,如果”,那么你可能需要做的提供标志来检查条件中的其他
可能性1

void checkRoom(int x) 
{ 


    if ((x >= 203) && (x <= 216)) 
    { 
     printf("first quad\n"); 
     flag=1; 
    } 



    if ((x >=217) && (x <= 229)) 
    { 
     printf("second quad\n"); 
     flag=1; 
    } 


    if ((x >=232) && (x <= 238)) 
    { 
     printf("CSL\n"); 
      flag=1; 
    } 


    if ((x >= 246) && (x <= 257)) 
    { 
    printf("classroom wing\n"); 
     flag=1 ; 
    } 

    else if(flag !=1) 
    { 
     printf("not sure\n"); 
    } 


} 

POSSIBILITY 2

使用开关盒。