我在链接中提供了以下图像代码。在n = 2之后,我无法迭代空格,请引导我完成操作

时间:2018-10-06 05:18:12

标签: c

我是stackoverflow的新手,所以请原谅任何错误。

#include void main() { int i,j,p,n,s; printf("Enter rows:\n"); scanf("%d",&n); if(n==1) { for(i=1;i<=n+2;i++) { for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } } else { for(i=1;i<=3;i++) { for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } for(p=1;p<=n-1;p++) { for(i=1;i<=2;i++) { for(j=3;j<=3;j++) { printf(" "); for(j=1;j<=i+1;j++) { printf("* "); } } printf("\n"); } } } }

enter image description here

2 个答案:

答案 0 :(得分:0)

以下是代码的发布方式:

每行代码前面有4个空格

@Configuration
public class TestBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equalsIgnoreCase("requestMappingHandlerAdapter")) {
            RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter) bean;
            List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
            List<HandlerMethodArgumentResolver> modifiedArgumentResolvers = new ArrayList<>(argumentResolvers.size());                
            for(int i =1; i< argumentResolvers.size();i++){
                modifiedArgumentResolvers.add(argumentResolvers.get(i));
            }
            modifiedArgumentResolvers.add(new TestRequestBodyMethodProcessor(requestMappingHandlerAdapter.getMessageConverters(), new ArrayList<Object>()));
            ((RequestMappingHandlerAdapter) bean).setArgumentResolvers(null);
            ((RequestMappingHandlerAdapter) bean).setArgumentResolvers(modifiedArgumentResolvers);
        }
        return bean;
    }
}

public class TestRequestBodyMethodProcessor extends AbstractMessageConverterMethodProcessor {

    public TestRequestBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        super(converters);
    }

    public TestRequestBodyMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager) {
        super(converters, manager);
    }

    public TestRequestBodyMethodProcessor(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice) {
        super(converters, null, requestResponseBodyAdvice);
    }

    public TestRequestBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
                                          ContentNegotiationManager manager, List<Object> requestResponseBodyAdvice) {
        super(converters, manager, requestResponseBodyAdvice);
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestBody.class);
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||
                returnType.hasMethodAnnotation(ResponseBody.class));
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        parameter = parameter.nestedIfOptional();
        BaseInsert trans_type_code = ;
        Object arg = readWithMessageConverters(webRequest, parameter,
                Test.getModelClassObject().getClass());
        String name = Conventions.getVariableNameForParameter(parameter);

        WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
        if (arg != null) {
            validateIfApplicable(binder, parameter);
            if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
            }
        }
        mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());

        return adaptArgumentIfNecessary(arg, parameter);
    }

    @Override
    protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
                                                   Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);
        Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
        if (arg == null) {
            if (checkRequired(parameter)) {
                throw new HttpMessageNotReadableException("Required request body is missing: " +
                        parameter.getMethod().toGenericString());
            }
        }
        return arg;
    }

    protected boolean checkRequired(MethodParameter parameter) {
        return (parameter.getParameterAnnotation(RequestBody.class).required() && !parameter.isOptional());
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType,
                                  ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
            throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

        mavContainer.setRequestHandled(true);
        ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
        ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
        // Try even with null return value. ResponseBodyAdvice could get involved.
        writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
    }
}

但是,当您的代码(及以上代码)通过编译器运行时,结果如下:

#include <stdio.h>

int main( void ) 
{ 
    int i,j,p,n,s; 

    printf("Enter rows:\n"); 
    scanf("%d",&n); 

    if(n==1) 
    { 
        for(i=1;i<=n+2;i++) 
        { 
            for(j=1;j<=i;j++) 
            { 
                printf("* "); 
            } 

            printf("\n"); 
        } 
    } 

    else 
    { 
        for(i=1;i<=3;i++) 
        { 
            for(j=1;j<=i;j++) 
            { 
                printf("* "); 
            } 

            printf("\n"); 
        } 

        for(p=1;p<=n-1;p++)
        { 
            for(i=1;i<=2;i++) 
            { 
                for(j=3;j<=3;j++) 
                { 
                    printf(" "); 

                    for(j=1;j<=i+1;j++) 
                    { 
                        printf("* "); 
                    } 
                } 

                printf("\n"); 
            } 
        } 
    }
}

您真的不希望您的代码产生任何警告或任何错误。

为便于阅读和理解:

  1. 一致地缩进代码。在每个大括号“ {”之后缩进。在每个右大括号'}'之前不要缩进。建议每个缩进级别为4个空格。
  2. 遵循公理:每行仅一个语句,每个语句(最多)一个变量声明。
  3. 变量名称(和参数名称)应指示gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" untitled.c: In function ‘main’: untitled.c:5:14: warning: unused variable ‘s’ [-Wunused-variable] int i,j,p,n,s; ^ Compilation finished successfully. content(或更好的是,两者都显示)
  4. 在括号内,括号内,花括号内,逗号后,分号后,C运算符周围插入合理的空间

请注意,usage函数只有两个有效签名。这两个签名的返回类型均为main()

不发布图像。而是将实际文本复制并粘贴到您的问题中

您是什么意思:不能迭代空格

答案 1 :(得分:0)

哇。

我花了一些时间来真正了解您想要的东西。 在下面,您可以查看缩进,注释和工作代码。我试图尽可能地保留它。

#include <stdio.h>

int main() {
    int i, /* used for "* " when n == 1 (columns),
              used for counting rows for the first block when n != 1*/
        j, /* used for "* " when n != 1 (columns),
              used for counting rows for the first block when n != 1*/
        p, /* used to print all the blocks after n = 1, when n != 1 */
        n, /* input from user; number of blocks to print */
        s; /* unused !!!! */

    printf("Enter rows:\n");

    scanf("%d", &n);

    // WARNING! 'n' can be zero. That would break this whole thing.
    if(n==1) {
        for(i = 1; i <= n + 2; i++) {

            for(j = 1; j <= i; j++) {
                printf("* ");
            }

            printf("\n");
        }
    } else {

        // Draw first block; 'i' = rows, 'j' = columns
        // This could be solved with only 'i'...
        for(i = 1; i <= 3; i++) {

            for(j = 1; j <= i; j++) {

                printf("* ");

            }
            printf("\n");
        }

        // Draw the latter blocks
        // Could simpy do "p = 0; p <= n", instead?
        for(p = 1; p <= n-1; p++) {

            // Loop twice, second and 2rd row of the block:
            //
            // * *
            // * * *

            for(i = 1; i <= 2; i++) {

                // Loop once? What?!
                for(j = 3; j <= 3; j++) {

                    // I inserted this here; this solves your problem, "indentation" works fine
                    // I actually make use of 's'! Yiyy!
                    for (s = 1; s <= p; s++) {
                        printf("    ");
                    }

                    // Draw the columns
                    for(j = 1; j <= i + 1; j++) {
                        printf("* ");
                    }

                }

                printf("\n");
            }
        }
    }
}

我完全同意User3629249answer,请尝试遵循可读性规则。 您可能还想尝试调试器,无论是Visual Studio调试器还是gdb。另外,请输入变量名!

今天过得愉快,
-Colda