首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用“if is”表达式来生成一个逐字拼凑的句子,从而生成整个句子

如何使用“if is”表达式来生成一个逐字拼凑的句子,从而生成整个句子
EN

Stack Overflow用户
提问于 2021-03-23 21:10:45
回答 1查看 45关注 0票数 0

我正在编写一些代码,其中我想(逐字逐句地)将以下句子的变体拼凑在一起,但我正在处理我的“if”和“else if”的用法

代码语言:javascript
复制
You are in a twisting little maze of passages, all different.

事实上,我总共有12句话要说:

代码语言:javascript
复制
        You are in a twisting little maze of passages, all different.
        You are in a little twisting maze of passages, all different.
        You are in a twisty little maze of passages, all different.
        You are in a little twisty maze of passages, all different.
        You are in a little maze of twisting passages, all different.
        You are in a little maze of twisty passages, all different.
        You are in a twisting maze of little passages, all different.
        You are in a twisty maze of little passages, all different.
        You are in a maze of twisting little passages, all different.
        You are in a maze of little twisting passages, all different.
        You are in a maze of twisty little passages, all different.
        You are in a maze of little twisty passages, all different.

我使用的变量如下:

代码语言:javascript
复制
1) int little  /  2) int twist  /  3) int twisting   / 4) int before

它们的职能如下:

  1. 在以上句子中首先考虑的性质是形容词“小”是放在名词“迷宫”之前,还是在名词“段落”之前。换句话说,什么是小的,迷宫还是段落?这面旗帜将被称为“小”。这是函数的第一个参数。如果“小”的值是0,那么“小”就出现在“迷宫”之前,就像上面的前六句话一样。如果“小”的值是1,那么“小”就在“段落”之前,就像上面的最后六句话一样。--

  1. 第二个属性类似于第一个属性。这个性质是指扭形形容词(“扭”或“扭”)是出现在名词“迷宫”之前还是在名词“段落”之前。这面旗帜将被称为“扭曲”。这是函数的第二个论点。如果“扭曲”的值为0,那么扭转形容词就会出现在“迷宫”之前。如果“扭曲”的值是1,那么扭转形容词就在“段落”之前。--

  1. 第三个属性是扭形形容词是“扭”还是“扭”。这面旗帜将被称为“扭曲”。这是函数的第三个论点。如果“扭”的值是0,那么扭转形容词就是“扭”。如果“扭”的值是1,那么扭形形容词就是“扭”。--

  1. 第四个性质是形容词“小”是出现在扭形形容词之前还是后面,如果两个形容词都修改了同一个名词。这面旗帜将被称为“前面”。这是函数的第四个论点。如果“前面”的值为0,那么“小”则出现在“扭”形容词之后。如果“前面”的值是1,那么“小”就会出现在扭形形容词之前。当两个形容词不在同一个名词前面时,这个标志没有任何区别,这就是为什么只有12个不同的句子。

我有以下思路:

代码语言:javascript
复制
#include <stdio.h>

void mazesentence(int little, int twist, int twisting, int before)
{
    
    printf("You are in a ");
    
    
    if(little == 0 && twist == 0)
    {
        if(before == 0)
        {
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1);
                printf("twisting ");
            printf("little ");
        }
        else if(before == 1)
            printf("little ");
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1);
                printf("twisting ");
        printf("of maze ");
    }
           
    
    if(little == 1 && twist == 1)
    {
        if(before == 0)
        {
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1);
                printf("twisting ");
            printf("little");
        }
        else if(before == 1)
            printf("little ");
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1);
                printf("twisting ");
        printf("passages");
    }
        
    printf("passages, all different.\n");

    return;
}

int main( )
{
    mazesentence(0, 0, 1, 0);
    mazesentence(0, 0, 1, 1);
    mazesentence(0, 0, 0, 0);
    mazesentence(0, 0, 0, 1);
    mazesentence(0, 1, 1, 0);
    mazesentence(0, 1, 0, 0);
    mazesentence(1, 0, 1, 0);
    mazesentence(1, 0, 0, 0);
    mazesentence(1, 1, 1, 0);
    mazesentence(1, 1, 1, 1);
    mazesentence(1, 1, 0, 0);
    mazesentence(1, 1, 0, 1);

    return 0;
}

但是,输出如下:

代码语言:javascript
复制
You are in a twisting little twisting of maze passages, all different.
You are in a little twisting of maze passages, all different.
You are in a twisty twisting little twisty twisting of maze passages, all different.
You are in a little twisty twisting of maze passages, all different.
You are in a passages, all different.
You are in a passages, all different.
You are in a passages, all different.
You are in a passages, all different.
You are in a twisting littletwisting passagespassages, all different.
You are in a little twisting passagespassages, all different.
You are in a twisty twisting littletwisty twisting passagespassages, all different.
You are in a little twisty twisting passagespassages, all different.

这12句话需要与上述12句话相匹配。除了额外的“扭曲”,第一句几乎是一样的。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-24 00:59:57

代码中有一些错误,逻辑中也有其他错误:

  1. 'else if‘由于';’没有做任何事情,它之后的printf每次都会出现。

否则如果(扭曲== 1);打印(“扭曲");

这是正确的使用方法:

否则,如果(扭转== 1) printf(“扭曲");

  1. 在逻辑上也有一些问题,所以会有时间它什么也不做。--

我稍微修改了一下您的代码,它正在工作:

代码语言:javascript
复制
void mazesentence(int little, int twist, int twisting, int before)
{

printf("You are in a ");


if(twist == 0)
{
    if(before == 0){
        if (twisting == 0){
            printf("twisty ");
        } else if (twisting == 1){
            printf("twisting ");
        }
        if(little == 0){
            printf("little ");  
        }
    }else if(before == 1){
        if(little == 0){
            printf("little ");  
        }
        if(twisting == 0)
            printf("twisty ");
        else if(twisting == 1)
            printf("twisting ");
    }
    printf("maze of ");
    if(little == 1){
        printf("little ");
    }
}else if(twist == 1){
    
    if(little == 0){
        printf("little maze of ");
        if(before == 1){
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1)
                printf("twisting ");
        }
    }else if(little == 1){
        printf("maze of ");
        if(before == 1){
            printf("little ");  
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1)
                printf("twisting ");
        }else if(before == 0){
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1)
                printf("twisting ");
            printf("little ");  
        }
    }
}
    
printf("passages, all different.\n");

return;
}

int main( )
{
    mazesentence(0, 0, 1, 0);
    mazesentence(0, 0, 1, 1);
    mazesentence(0, 0, 0, 0);
    mazesentence(0, 0, 0, 1);
    mazesentence(0, 1, 1, 1);
    mazesentence(0, 1, 0, 1);
    mazesentence(1, 0, 1, 0);
    mazesentence(1, 0, 0, 0);
    mazesentence(1, 1, 1, 0);
    mazesentence(1, 1, 1, 1);
    mazesentence(1, 1, 0, 0);
    mazesentence(1, 1, 0, 1);

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66771351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档