首页 > 解决方案 > 需要帮助压缩 C 中的嵌套 if 语句

问题描述

你好,我终于得到了这个代码的工作版本,所有正确的响应,但它看起来仍然不是很好,我知道这有点糟糕。我正在尝试做的是仅当变量在某个范围内时才使某些操作可用。我已经看到使用看起来像的 if 语句完成了此操作

if(x > 0 || x < 10)

或者

if(x > 0 && x < 10)

但是每当我实现这些时,它不会给我一条错误消息,而是永远循环提示或似乎根本没有区别?当我尝试对这些量词使用 do/while 循环时,也会发生同样的事情。所以你可以看到我不得不嵌套一堆 if 循环分别检查每个量词。错误的代码。那么有没有办法让它少吸一点呢?谢谢

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

void fortune1(void);
void fortune2(void);

int main (void)
{
    
    int count;
    int num1;
    int num2; 
    char color;

    
    //get color
    printf("Color?\n");
    scanf("%s", &color);
    count = strlen(&color);                               
    
    if(count % 2 == 0)                                     //if even, load screen1. screen1 values = 1,2,3,4
    { 
        printf("Pick a number from 1 to 4.\n");
        scanf("%d", &num1);                                //remember, must be in range 1-4. program this later.
        
            if(num1 < 5)
            {
                if(num1 > 0)
                {
                    if(num1 % 2 == 0) 
                    {
                        fortune1(); 
                    }
                    else
                    {
                        fortune2();
                    }
                }
                else
                {
                    printf("Invalid\n");
                }
            }
            else
            {
                printf("Invalid\n");
            }
    }
    else                                                 //if odd, load screen 2. screen2 values = 5,6,7,8
                                                         //get new value
    {
        printf("Pick a number from 5 to 8.\n");
        scanf("%d", &num1);                                //remember, must be in range 1-4. program this later.
        
            if(num1 < 9)
            {
                if(num1 > 4)
                {
                    if(num1 % 2 == 0) 
                    {
                        fortune1(); 
                    }
                    else
                    {
                        fortune2();
                    }
                }
                else
                {
                    printf("Invalid\n");
                }
            }
            else
            {
                printf("Invalid\n");
            }
    }
}


// fortune1 function
void fortune1(void)
{         
        int fort;

    printf("Pick a number from 1 to 4.\n");
    scanf("%d", &fort);
            
        if(fort == 1){
            printf("fortune1\n");
        }
        else if(fort == 2){
            printf("fortune2\n");
        }
        else if(fort == 3){
            printf("fortune3\n");
        }
        else if(fort == 4){
            printf("fortune4\n");
        }
        else{
            printf("Invalid\n");
        }
        
}

    
// fortune2 function

void fortune2(void)
{     
    int tune;

    printf("Pick a number from 5 to 8.\n");
    scanf("%d", &tune);
            
        if(tune == 5){
            printf("fortune5\n");
        }
        else if(tune == 6){
            printf("fortune6\n");
        }
        else if(tune == 7){
            printf("fortune7\n");
        }
        else if(tune == 8){
            printf("fortune8\n");
        }
        else{
            printf("Invalid\n");
        }
}

标签: c

解决方案


你可能从未见过:

if(x > 0 || x < 10)

因为它总是true

顺便说一句,所有这些:

if(num1 < 5)
{
      if(num1 > 0)
      {
           if(num1 % 2 == 0) 
           {
               fortune1(); 
           }
           else
           {
               fortune2();
           }
       }
       else
       {
           printf("Invalid\n");
       }
   }
   else
   {
       printf("Invalid\n");
   }

可以改写为:

if(num1 < 5 && num1 > 0){
   if(num1 % 2 == 0)fortune1(); 
   else fortune2();
} else printf("Invalid\n");

和这个

    if(tune == 5){
        printf("fortune5\n");
    }
    else if(tune == 6){
        printf("fortune6\n");
    }
    else if(tune == 7){
        printf("fortune7\n");
    }
    else if(tune == 8){
        printf("fortune8\n");
    }
    else{
        printf("Invalid\n");
    }

可以使用重写switch

switch(tune){
     case 5: 
          // do something
          break;
     case 6: 
          // do something
          break;
     case 7: 
          // do something
          break;
     case 8: 
          // do something
          break;
     default:
          printf("Invalid\n");
}

推荐阅读