如何用重复数字C#绘制金字塔 [英] How to draw pyramid with rows of repeating numbers C#

查看:200
本文介绍了如何用重复数字C#绘制金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#Sharp中编写程序以创建金字塔图案,如下所示。金字塔的顶部以1开头,第二行包含两个2,然后是三个3,依此类推。用户必须提供行数作为输入。如果用户的输入为4,则会出现以下输出。

Write a program in C# Sharp to create a pyramid pattern as shown below. The top of the pyramid starts with a 1, the second row contains two 2s, then three 3s and so on. The user must provide the number of rows as input. The output below will appear if the user’s input is 4.

   1 
  2 2 
 3 3 3 
4 4 4 4





我的尝试:





What I have tried:

//Declare Main Variables
            int noOfRows;

if (radioButton1.Checked)
            {
                //Convert Text Box TOINT32
                noOfRows = Convert.ToInt32(textBox1.Text);
                string line = "" ; // for individual output
                
                for (int i = 1; i <= noOfRows; i++) 
                {
                    line = line + i;

                    richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                    richTextBox1.Text += line + "\n";
                    

                }
            }





我的代码告诉我这个结果





my code shows me this result

   1
  1 2
 1 2 3
1 2 3 4

推荐答案

我建​​议从这里开始: C#中的10个不同的数字模式程序? CsharpStar [ ^ ]

在那里你会找到10种不同的金字塔建筑方式的解释。你需要使用模式2.



祝你好运!
I'd suggest to start here: 10 different Number Pattern Programs in C# – CsharpStar[^]
There you'll find an explanation of 10 different ways of building pyramid. You need to use pattern 2.

Good luck!


想想你的其他程序(金字塔钻石)。你需要改变什么来换取每行星数的星星?



当你不明白你的代码在做什么或为什么它做什么确实如此,答案是调试器

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



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



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

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

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

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Think about your other program (pyramid Diamond). What would you have to change to replace stars with the number of of stars per line ?

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天全站免登陆