首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C程序,它读取值并将其用作钻石的参数。

问C程序,它读取值并将其用作钻石的参数。
EN

Stack Overflow用户
提问于 2016-09-26 22:32:29
回答 1查看 103关注 0票数 2

我需要编写一个程序来提示一个奇数正整数,并在由星号组成的页面上生成一个菱形;这个数字是高度和宽度。

我不知道在为奇数和正数设定参数之后该怎么办.我不太确定如何使用imut值并使用该值设置菱形图。

我有一个基本的钻石代码,但我不知道它有什么问题。

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

int main (void) 
{
    int row, col, numstars;
    int half, rate = 1;

    printf("Please enter a positive odd integer.\n");

    scanf("%d", &numstars);

    for (row = 1; row <= numstars; row++) 
    {
        half = numstars / 2;

        for (col = 0; col < half + 1 - numstars; col++)
            printf(" ");

        for (col = 1; col <= 2 * numstars - 1; col++)
            printf("*");

        if ((numstars == (half + 1)))
            rate = -rate;
        numstars = numstars + rate;

        printf("\n");       
    }
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2016-09-29 13:21:59

你让我们在这里教你算法的基本知识。

  • 你必须对你的问题更加具体。
  • 您必须学习如何调试--互联网上存在大量的资源,但最简单的方法是从初学者的开始。

最大的问题是,在numstar循环中修改for .所以你得到一个无限循环..。

下面是我将如何修复您的代码:使其简单并区分两种情况

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

int main (void)
{
    int row, col, numstars;
    int half, rate=1;

    printf("Please enter a positive odd integer.\n");
    scanf("%d", &numstars);

    half=(numstars-1)/2;
    for (row=0; row<numstars;row++)
    {
      if(row <= half) {
        for (col=0; col<  half-row;          ++col) printf(" ");
        for (     ; col<= half+row;          ++col) printf("*");
      } else {
        for (col=0; col<  row-half;          ++col) printf(" ");
        for (     ; col<  numstars+half-row; ++col) printf("*");
      }
      printf("\n");
    }
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39713416

复制
相关文章

相似问题

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