如何像钻石一样绘制图案C# [英] How to draw pattern like a diamond C#

查看:145
本文介绍了如何像钻石一样绘制图案C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C#编写程序以显示如下所示的菱形图案。用户提供钻石中间的行号。请注意,行中的星数分别为1,3,5等,然后向下...... .5,3,1

 * 
***
*****
*******
*****
***
*



请注意所有星星都居中。



我尝试了什么:



  if (radioButton2.Checked) / /  完整的钻石 
{
int i,j,k ,n;
// 转换文本框TOINT32
n = Convert.ToInt32(textBox1.Text );
string line = ;单个输出的 //

for (i = 1 ; i < = n; i ++) // 上钻石
{

for (k = 1 ; k < = ni; k ++)
{
line = ;
}
for (j = 1 ; j < = i; j ++)
{
line + = *;
}
richTextBox1.SelectionAlignment = Horizo​​ntalAlignment.Center;
richTextBox1.Text + = line + \ n;
}
for (i = 1 ; i < = n - 1 ; i ++) // 底部钻石
{
(k = 1 ; k < = i; k ++)
{
line = ;
}
for (j = n - 1 ; j > = i; j--)
{
line + = *;
}
richTextBox1.SelectionAlignment = Horizo​​ntalAlignment.Center;
richTextBox1.Text + = line + \ n;
}

}





我的结果是

 * 
* *
* * *
* * * * * * *
* * *
* *
*



请注意所有星星都居中



我的钻石在1 2 3 7和3 2中出错了1 formart。请帮助

解决方案



这一行有一个错误:

 for(i = 1; i< = n; i ++)// Upper Diamond 

应该是:

 for(i = 1; i< n; i + = 2)//上钻石 - 比较和增量的变化

同样适用于底部钻石。钻石需要进行一些调整;我把它留给你,因为我只能引导你找到解决方案,但是你需要做功课。



必须在for循环中使用必要的线路清理。

 for(k = 1; k< = ni; k ++)
{
line =;
}

这可以只是一行; FOR循环不必要地惩罚编译器几次做同样的事情。

 line =; 


我将从分析发生的事情开始。在我看来,有许多固定长度的线。每行的长度等于总行数。线的数量必须是奇数且> 1,因此您有一条中间线来给出钻石效果。最初有一个中心的'*'字符,然后随着行号的增加,新的*被添加到现有星的左侧和右侧。这一过程一直持续到中间线被打印出来,然后过程被反转,最近添加的*左边和右边然后用空格替换。


虽然字符串是 char 数组,它是只读的。但是,您可以使用 char [] ,对其进行操作,然后在打印之前将其转换为字符串。类似

  var  currentLine =  new   string ' ' ,线);  //  带空格的填充行 
char [] characters = currentLine.ToCharArray();



然后你可以将'*'添加到字符通过索引到它的数组。你需要一个 leftposition rightposition 变量以及一个增量变量。打印中间行后,增量设置为-1

 characters [lpos] = charToPaste; 
characters [rpos] = charToPaste;
Console.WriteLine( new string (characters));
lpos - =增量;
rpos + =增量;



您只需要一个循环即可完成此操作。


< blockquote>仔细检查清除变量的位置。



当你不明白你的代码是什么时做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Write a program in C# to display the diamond pattern shown below. The user provides the number of the row that is the middle of the diamond. Note that the number of stars in the rows are are 1, 3, 5, etc respectively and then downwards ….5, 3, 1.

   *   
  *** 
 ***** 
******* 
 ***** 
  *** 
   *


Please note all stars are centered.

What I have tried:

if (radioButton2.Checked) //full diamond
            {
                int i, j, k, n;
                //Convert Text Box TOINT32
                n = Convert.ToInt32(textBox1.Text);  
                string line = ""; // for individual output

                for (i = 1; i <= n; i++)//Upper Diamond
                {
                    
                    for (k = 1; k <= n-i; k++) 
                    {
                        line = "";
                    }
                    for (j = 1; j <= i; j++)
                    {
                        line +=  "* " ;
                    }
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                    richTextBox1.Text += line + "\n";
                }
                for (i = 1; i <= n - 1; i++) //Bottom Diamond
                {
                    for (k = 1; k <= i; k++)
                    {
                        line = "";
                    }
                    for (j = n - 1; j >= i; j--)
                    {
                        line += "* ";
                    }
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                    richTextBox1.Text += line + "\n";
                }

            } 



My results is

      *  
     * * 
    * * * 
* * * * * * * 
    * * * 
     * * 
      *


Please note all stars are centered

my diamond comes out wrong in a 1 2 3 7 and 3 2 1 formart. please help

解决方案

Hi ,
There is a mistake in this line:

for (i = 1; i <= n; i++)//Upper Diamond

It should be:

for (i = 1; i < n; i+=2)//Upper Diamond - change in comparison and increment

Same applies to the bottom diamond. There needs to be a couple of adjustments for the bottom diamond; I leave that up to you as I can only guide you to the solution, but you need to do the homework.

Plus line cleaning involved in the for loop is unnecessary.

for (k = 1; k <= n-i; k++) 
{
   line = "";
}

This can simply be a single line; the FOR loop is unnecessarily punishing the compiler to do the same thing for a couple of times.

line = "";


I would start off by analysing what is going on. As I see it, there are a number of fixed-length lines. The length of each line is equal to the total number of lines. The number of lines needs to be an odd number and >1 so that you have a middle line to give the diamond effect. Initially there is a central ‘*’ character and then a new * is added to the left and right of the existing stars as the line number increases. This carries on until the middle line has been printed, then the process is reversed and the most recently added * to the left and the right is then replaced by a space.

Although a string is char array, it is read only. You can, however, use a char[], manipulate that and then convert it to a string before printing it. Something like

var currentLine = new string(' ', lines); //fill line with spaces
char[] characters = currentLine.ToCharArray();


You can then add ‘*’ to the characters array by indexing into it. You will need a leftposition and rightposition variable along with an increment variable. The increment is set to -1 after the middle line is printed.

characters[lpos] = charToPaste;
characters[rpos] = charToPaste;
 Console.WriteLine(new string(characters));
lpos -= increment;
rpos += increment;


You need only a single loop to do this.


Check carefully where you clear the line variable.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何像钻石一样绘制图案C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆