6星(大卫之星/大卫盾)在星号和字母

时间:2014-12-30 20:14:28

标签: c# geometry

我想在控制台应用程序中打印一个Star of David形状:

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

我想支持两种模式:

  1. 给定一个整数N,使用N个星号绘制星号。
  2. 给定字符串S,使用字符串中的字符绘制星号。
  3. 以下是我目前的情况:

    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)。

    只是为了澄清我的问题:考虑到一些星号,我想知道两个相对三角形的高度和基本尺寸是多少。另外,从哪里开始打印相反的三角形?每条印刷线应该有多少个星号?

3 个答案:

答案 0 :(得分:3)

一个选项是创建一个二维数组,在这里您希望定位星星,然后在数组填充之后再迭代它并将其写入屏幕。例如,

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将光标定位在您要写入*的位置。

答案 1 :(得分:2)

我知道这篇文章很老但是对于即将到来的开发者来说。

代码可能并不漂亮,但它正在发挥作用。

`

d3.scaleOrdinal()

`

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

答案 2 :(得分:1)

请查看此内容以了解第一个问题的基本表单: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>