注册 登录
编程论坛 C# 论坛

新手求教,输出一个9*9的数组

番茄暴走123 发布于 2016-03-08 17:11, 4385 次点击
static void Main(string[] args)
        {
            string[,] array = new string[9, 9];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    array[i, j] = ".";
                    Console.Write(array[i, j]);
                }
                Console.ReadLine();

            }

请教,输出的9*9数组,只显示了一行,必须手动打个回车才能输出下一行。百度了一下,\r\n可以用,可是改成 Console.ReadLine("\r\n"),程序总出错,为啥子呢
16 回复
#2
vxman2016-03-08 19:54
因为你有 Console.ReadLine();这一行代码,所以每输出一行需要打一下回车,把这个删除就好了
#3
番茄暴走1232016-03-10 10:58
还是不对呢,怎模办
 static void Main(string[] args)
        {
            string[,] array = new string[9, 9];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    array[i, j] = ".";
                    Console.Write(array[i, j]);
                }
                Console.Write('\n');      
            }
                        // this is for get input, which will stop the screen
        }
#4
over12302016-03-10 11:31
string[,] array = new string[9, 9];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    array[i, j] = ".";
                    Console.Write(array[i, j]);
                }
                Console.WriteLine();
            }
            Console.ReadLine();
#5
番茄暴走1232016-03-10 15:33
谢谢
#6
ggj20162016-03-11 15:02
学习 了
#7
番茄暴走1232016-03-21 09:54
继续求教,这个数组如果用函数的方式写的话,怎么写呢
#8
番茄暴走1232016-03-21 10:38
class Program
    {
        public void GeneratePointMatrix(int x, int y,string Value)
        {
            for (int i=0;i<9;i++)
            {
                for (int j=0;j<9;j++)
                {
                    int x=int.Parse(Value);
                    int y=int.Parse(Value);
                }
            }
        }  
       static void Main(string[],args)
       {
           GeneratePointMatrix();
           String Value=".";
           Console.WriteLine();
           Console.ReadLine();
       }
    }

Error    5    The type or namespace name 'args' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    21    34    ConsoleApplication13
Error    6    No overload for method 'GeneratePointMatrix' takes 0 arguments    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    23    12    ConsoleApplication13
Error    1    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    21    33    ConsoleApplication13
Error    2    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    21    38    ConsoleApplication13
Error    4    A local variable named 'y' cannot be declared in this scope because it would give a different meaning to 'y', which is already used in a 'parent or current' scope to denote something else    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    17    25    ConsoleApplication13
Error    3    A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a 'parent or current' scope to denote something else    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    16    25    ConsoleApplication13
#9
qq10235692232016-03-21 10:43
你这个程序要实现什么,感觉一点用都没有,错误一大堆,建议再学习吧。参考下下面的看看。
程序代码:
class Program
{   
    public void GeneratePointMatrix( )
    {
        int k=0;
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                k=i*j;
                Console.Write(k.ToString()+"  ");
            }
            Console.WriteLine();
        }
    }  
      
    static void Main(string[] args)
    {
        GeneratePointMatrix();
        
        Console.ReadKey();
    }
}


[此贴子已经被作者于2016-3-21 10:59编辑过]

#10
番茄暴走1232016-03-21 11:00
回复 9楼 qq1023569223
谢谢版主,就是想输出一个9×9数组,赋值都是"."。
#11
番茄暴走1232016-03-21 11:05
回复 9楼 qq1023569223
        public string Value;
        public void GeneratePointMatrix(int x, int y)
        {
            for (int i=0;i<9;i++)
            {
                for (int j=0;j<9;j++)
                {
                    int x=int.Parse(Value);
                    int y=int.Parse(Value);
                    String Value=".";
                }
            }
        }  
       static void Main(string[],args)
       {
           int i=9,int j=9;
           GeneratePointMatrix();
           Console.WriteLine();
           Console.ReadLine();
        }

Error    3    Identifier expected; 'int' is a keyword    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    25    20    ConsoleApplication13
Error    1    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    23    33    ConsoleApplication13
Error    2    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    23    38    ConsoleApplication13
还是有3个错误,怎么弄呢

      

[此贴子已经被作者于2016-3-21 11:12编辑过]

#12
over12302016-03-21 11:22
int.Parse  是将string 类型转换成 int ,如果string 为null 或者无法转换,就会报错。
你的Value 开始就是null ,用 int.Parse当然会报错
#13
qq10235692232016-03-21 11:23
我想不明白你怎么想的,感觉你一点都不会C#。
程序代码:
public string Value=".";

public void GeneratePointMatrix(int x, int y)
{
    for (int m=1;m<=x;m++)
    {
        for (int n=1;n<=y;n++)
        {
            Console.Write(Value+"  ");
        }
        Console.WriteLine();
    }
}  

static void Main(string[] args)
{
    int i=9,j=9;
    GeneratePointMatrix(i,j);
    Console.WriteLine();
    Console.ReadKey();
}
#14
番茄暴走1232016-03-21 14:34
回复 13楼 qq1023569223
是的,确实不懂,谢谢您的指教。可是,我用您的代码输进去后,怎么还是出现两个错误呢?
Error    1    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    22    33    ConsoleApplication13
Error    2    Identifier expected    C:\Users\zhanglei\Documents\Visual Studio 2010\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs    22    38    ConsoleApplication13
#15
lantianke2016-03-26 17:22
    class Program
    {
        static void Main(string[] args)
        {
            int k = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write("["+i.ToString()+","+j.ToString()+"]");
                }
                Console.WriteLine("  ");
            }
        }
    }

你看下
#16
番茄暴走1232016-03-28 16:11
回复 15楼 lantianke
谢谢亲
#17
yzh000012016-04-04 09:22
学习了!!
1