6星(大卫星/大卫盾牌)在星群和字符串字母中

本文关键字:字符串 盾牌 | 更新日期: 2023-09-27 18:35:45

我想在控制台应用程序中打印大卫之星形状:

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

我想支持两种模式:

  1. 给定一个整数 N,使用 N 星号绘制星星。
  2. 给定一个字符串 S,使用字符串中的字符绘制星号。

这是我目前拥有的:

static void Triangle(int nBase)
{
    int i, j, move;
    for (i = 1; i <= nBase; i++)
    {
        move = 0;
        do
        {
            Console.Write(" ");
            move++;
        } while (move <= nBase * 3 - i + 1);
        for (j = 1; j <= i; j++)
        {
            Console.Write("* ");
        }
        Console.WriteLine();
    }
}
static void TriangleInverted(double nBase)
{
    int i, j, move;
    for (i = (int)nBase; i >= 0; i--)
    {
        move = 0;
        do
        {
            Console.Write(" ");
            move++;
        } while (move <= nBase * 3 - i + 1);
        for (j = 1; j <= i; j++)
        {
            Console.Write("* ");
        }
        Console.WriteLine();
    }
}

如您所见,我可以画一个三角形和一个倒三角形(根据模式 1),但我不知道如何将它们组合成大卫之星(基本上使它们正确重叠)。

我也不确定如何实现模式(2)。

只是为了澄清我的问题:给定一些紫苑,我想知道 2 个相对三角形中每个三角形的高度和底部大小是多少。此外,从哪里开始打印相反的三角形?每条印刷行中应该有多少个紫苑?

6星(大卫星/大卫盾牌)在星群和字符串字母中

一种选择是创建一个二维数组,列出您希望星星的位置,然后在它们填充数组后,迭代它并将其写入屏幕。例如

var points = new bool[10,10];
for (int x=0; x<10; x++)
for (int y=0; y<10; y++)
{
   if (x == y) points[x,y] = true;
}
for (int y=0; y<10; y++)
{
    for (int x=0; x<10; x++)
    {
       if (points[x,y]) Console.Write("*");
       else Console.Write(" ");
    }
    Console.WriteLine();
}

另一种选择是使用 SetCursorPosition 将光标定位到要写入*的位置。

我知道这篇文章很旧,但对于即将到来的开发人员来说。

代码可能不漂亮,但它正在工作。

'

public static void Main(string[] args)
        {
            int size=20;
            for(int i=0;i<size;i++){
                for(int j=0;j<size;j++){
                     if((i+j>=size/2-1 && i<=size/2-1 && j-i<=size/2-1) ||
                        (i==size/5 && j<size-1)||
                        (i-j<=size/5 && j<size/2 && i>size/5)||
                        (i+j<=size+((size-10)/5) && j>=size/2 && i>size/5))
                         Console.Write("*");
                     else
                         Console.Write(" ");
                }
                Console.WriteLine("");
            }
        }

'

根据需要更改大小(优先选择偶数)。

看看这个以了解你第一个问题的基本形式:http://jsfiddle.net/steinair/d6pe977v/

    <html>
Width of David star: <input type=text size=2 id=starwidth onchange="check_and_draw();" value="5" />
<center>
    <div id=star></div>
</center>
<script>
function check_and_draw(){
  w=document.getElementById("starwidth").value;
  drawstar(w);
}
function writetxt(t){
    document.getElementById("star").innerHTML+=t;
}
function drawstar(w){
document.getElementById("star").innerHTML="<br>Star of David with width of "+w+" letters:<hr>";
h=Math.ceil(w/3);//since every phase is a third of the triangle
for (y=1;y<=h;y++){ //The first third of the upper triangle
    for (x=1;x<=y;x++){
     writetxt("A ");
    }
    writetxt("<br>");    
}
for (y=0;y<h;y++){ //The base of the lower triangle and 2nd third of the upper triangle
    for (x=(w-y);x>0;x--){
     writetxt("B ");
    }
    writetxt("<br>");    
}
//if width is not even = odd then draw another line in the middle:
if ( w % 2 ) {
    for (x=(w-y);x>0;x--){
     writetxt("C ");
    }
    writetxt("<br>");
}    

for (y=h;y>0;y--){ //The base of the upper triangle and 2nd third of the lower triangle
    for (x=(w-y)+1;x>0;x--){
     writetxt("D ");
    }
    writetxt("<br>");    
}
for (y=h;y>0;y--){ //The last third of the lower triangle
    for (x=y;x>0;x--){
     writetxt("E ");
    }
    writetxt("<br>");    
}
}
</script>
</html>

这回答了你的第二个问题:http://jsfiddle.net/steinair/qo575L9d/

    <html>
String:
<input type=text size=30 id=starstring onchange="check_and_draw();" value="" />
<center>
    <div id=star></div>
</center>
<script>
function check_and_draw() {
    starstring = document.getElementById("starstring").value;
    w = starstring.length;
    drawstar(w, starstring);
}
function writetxt(t) {
    document.getElementById("star").innerHTML += t;
}
function drawstar(w, starstring) {
    var n = 0;
    var spacer = "&nbsp;&nbsp;&nbsp;";
    document.getElementById("star").innerHTML = "<br>Star of David from string '" + starstring + "' (" + w + " letters):<hr>";
    h = Math.ceil(w / 3); //since every phase is a third of the triangle
    for (y = 1; y <= h; y++) { //The first third of the upper triangle
        n = 0;
        for (x = 1; x <= y; x++) {
            charN = starstring.charAt(n);
            n++;
            writetxt(charN + spacer);
        }
        writetxt("<br>");
    }
    for (y = 0; y < h; y++) { //The base of the lower triangle and 2nd third of the upper triangle
        n = 0;
        for (x = (w - y); x > 0; x--) {
            charN = starstring.charAt(n);
            n++;
            writetxt(charN + spacer);
        }
        writetxt("<br>");
    }
    //if width is not even = odd then draw another line in the middle:
    if (w % 2) {
        n = 0;
        for (x = (w - y); x > 0; x--) {
            charN = starstring.charAt(n);
            n++;
            writetxt(charN + spacer);
        }
        writetxt("<br>");
    }

    for (y = h; y > 0; y--) { //The base of the upper triangle and 2nd third of the lower triangle
        n = 0;
        for (x = (w - y) + 1; x > 0; x--) {
            charN = starstring.charAt(n);
            n++;
            writetxt(charN + spacer);
        }
        writetxt("<br>");
    }
    for (y = h; y > 0; y--) { //The last third of the lower triangle
        n = 0;
        for (x = y; x > 0; x--) {
            charN = starstring.charAt(n);
            n++;
            writetxt(charN + spacer);
        }
        writetxt("<br>");
    }
}
</script>
</html>