回 帖 发 新 帖 刷新版面

主题:刚学C#,不懂正方形输出循环问题,大家帮帮迷途的我啦~~deng

using System;
using System.Collections.Generic;
using System.Text;

namespace ZUOYE2
{
    class Program
    {
        static void Main()
        {
        tuxingchose:
            Console.WriteLine("请选择要输出的图形 1.正方形 2.长方形 3.三角形");
            string tuxing = Console.ReadLine();
            if (tuxing != "1" && tuxing != "2" && tuxing != "3")
            {
                Console.WriteLine("输入错误!");
                goto tuxingchose;
            }
            if (tuxing == "1")
            {
                Console.WriteLine("请输入正方形的边长");
         
            int a = Int32.Parse(Console.ReadLine());
                if (a <= 0)
                {
                    Console.WriteLine("输入错误!");
               
                }
            kongshixin1:
                Console.Write("是否实心?按键Y或y为实心,N或n为空心:");
                string b = Console.ReadLine();//b为是否为实心
                if (b != "y" && b != "Y" && b != "n" && b != "N")
                {
                    Console.WriteLine("输入错误!");
                    goto kongshixin1;

                }
                Console.Write("选择什么符号表示(回车则为*):");
                string c = Console.ReadLine();//c为图形的符号表示

                if (c == "")
                    c = "*";

                [color=800000][b]for (int i = 0; i < a; i++)----->正方形不是四条边吗?
                {
                    for (int j = 0; j < a; j++)---->怎么只得i和j?输出怎样不是2条边而是四条边?究竟是怎样输出的?搞不懂                    {[/b][/color]                        if (b == "Y" || b == "y")
                            Console.Write(" " + c);
                        if (b == "N" || b == "n")
                        {
                            if (i == 0 || i == a - 1 || j == 0 || j == a - 1)
                                Console.Write(" " + c);
                            else
                                Console.Write("  ");
                        }
                    }
                    Console.WriteLine();//正方形完毕

                  


                }
            }
        }
    }
}

大家帮下忙喇,小弟实菜~~~

回复列表 (共4个回复)

沙发

没人?????

板凳

人呢????????????????有无人在线?????

3 楼

额。。。。。

4 楼

for (int i = 0; i < a; i++)----->正方形不是四条边吗?
                {
                    for (int j = 0; j < a; j++)---->怎么只得i和j?输出怎样不是2条边而是四条边?究竟是怎样输出的?搞不懂                    {                        if (b == "Y" || b == "y")
                            Console.Write(" " + c);
                        if (b == "N" || b == "n")
                        {
                            if (i == 0 || i == a - 1 || j == 0 || j == a - 1)
                                Console.Write(" " + c);
                            else
                                Console.Write("  ");
                        }
                    }
                    Console.WriteLine();//正方形完毕

                  


                }

当a的6时
第一个循环:for (int j = 0; j < a; j++)---->******
然后是Console.WriteLine();  换行
i++
第二个循环后结果是:******
                    ******
如此类推。
x 和y 可以看成坐标

我来回复

您尚未登录,请登录后再回复。点此登录或注册