2016-11-11 68 views
0

我一直在试图做一个我几天前收到的任务。基本上,任务是在C#控制台应用程序:c#使用2d数组输入的坐标打印“*”字符

提示符中,直到字用户为2个坐标输入“STOP”进入。一旦命中“stop”字样,在每个输入坐标处打印一个“*”(星号字符)。坐标打印的字段是20x20。我尝试过这样做,但无济于事。如果有人能帮助我,并告诉我如何存储输入X,Y为二维数组,那简直太好了:)

如何应用应该工作:http://imgur.com/a/SnC1k

的[0,5] [18,18]等是输入的坐标,后面打印在下面。 “#”字符不需要打印,他们在这里只是为了帮助理解任务。

我如何试图做到这一点,但没有奏效:

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     bool stopped = false; 
     int x=0; 
     int y=0; 


     while (stopped) 
     { 
      string[,] coordinates = Console.ReadLine(); 
      string response = Console.ReadLine(); 
      response = response.ToLower(); 

      if (response == "STOP") 
       stopped = true; 
      else 
      { 
       string[] xy = coordinates.Split(','); 
       x = int.Parse(xy[0]); 
       y = int.Parse(xy[1]); 

       Console.SetCursorPosition(x, y); 
       Console.Write("*"); 
      } 






     } 


    } 
    } 
    } 

的代码我只是没有做到这一点。它显示了多个我不知道如何解决的错误,我唯一能做的就是让它在我输入第一个坐标时立即打印一个字符,而不是在每个输入的坐标处打印一个字符,在STOP之后被击中。

+0

可能解决你很多问题的东西是我认为你意思是“while(!停止)' –

+1

您还将您的回应转换为小写,但是您将回应与“停止”而不是“停止”进行比较 –

+0

感谢您指出那些错误的人:) –

回答

0

因为你只需要显示一个矩阵式的输出,你不需要使用Console.SetCursorPosition(x, y);

你也应该以某种方式读取用户输入和设置给定位置上的正确值,而不是商店该坐标。

试试这个,让我知道如何为你的作品。你也可以在this小提琴上看到它。输入坐标作为两个空格分隔的数字并输入停止打印。

using System; 

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     int x=0; 
     int y=0; 
     char[,] coordinates = new char[20,20]; 

     while (true) 
     { 
      //user must enter 2 3 for example. 
      string[] response = Console.ReadLine().Split(new[]{" "}, StringSplitOptions.RemoveEmptyEntries); 
      if (response[0].ToLower() == "stop") 
       break; 

      x = int.Parse(response[0]); 
      y = int.Parse(response[1]); 

      coordinates[x,y] = '*'; 
     } 


     //Print the output 
     for(var i = 0; i < 20; i++) 
     { 
      for(var j = 0; j < 20; j++) 
       if (coordinates[i,j] == (char)0) 
        Console.Write('#'); 
       else 
        Console.Write(coordinates[i,j]); 

      Console.WriteLine(); 
     } 
    } 
} 

希望这有助于!

+0

Karel您的代码确实可行,我不得不改变输出中的for循环从1开始,所以它实际上将它打印在正确的位置上。虽然我在20,15年写作时,它只是崩溃,说这个指数甚至不是我认为它不是? –

+0

Yeap,你应该根据自己的需求来验证。 C#中的数组基于zore,所以在这种情况下,20,15是无效的位置,因为最后的位置是19,19 –

+0

非常感谢,您的编码技能真的是令人羡慕的水平。 :) –

1

创建一个20x20二维数组。提示用户输入x和y。一旦用户在数组[x,y]中每次存储一个1,然后如果为null或0,则打印'#',如果其打印'1',则打印'*'。沿着这些路线我没有检查这是否工作或编译,但应该让你在正确的轨道上。

int[,] grid = new int[20,20]; 
    while (!stopped) 
    { 
     string[,] coordinates = Console.ReadLine(); 
     string response = Console.ReadLine(); 
     response = response.ToUpper(); 

     if (response == "STOP") 
      stopped = true; 
     else 
     { 
      string[] xy = coordinates.Split(','); 
      x = int.Parse(xy[0]); 
      y = int.Parse(xy[1]); 
      grid[x,y] = 1; 
     } 

     for (int i = 0; i < 20; i++) 
     { 
      for (int j = 0; j < 20; j++) 
      { 
       if (grid[i, j] > 0) 
        Console.Write("*"); 
       else 
        Console.Write("#"); 
      } 
      Console.WriteLine(""); 
     } 
    } 
0

我建议分解这个任务,把整个rountine拼成一个单一的Main为坏习惯;你应该:

- ask user to enter coordinates 
- print out the map 

让我们提取方法:

private static List<Tuple<int, int>> s_Points = new List<Tuple<int, int>>(); 

private static void UserInput() { 
    while (true) { 
    string input = Console.ReadLine().Trim(); // be nice, let " stop " be accepted 

    if (string.Equals(input, "stop", StringComparison.OrdinalIgnoreCase)) 
     return; 

    // let be nice to user: allow he/she to enter a point as 1,2 or 3 4 or 5 ; 7 etc. 
    string[] xy = input.Split(new char[] { ',', ' ', ';', '\t' }, 
           StringSplitOptions.RemoveEmptyEntries); 
    int x = 0, y = 0; 

    if (xy.Length == 2 && int.TryParse(xy[0], out x) && int.TryParse(xy[1], out y)) 
     s_Points.Add(new Tuple<int, int>(x, y)); 
    else 
     Console.WriteLine($"{input} is not a valid 2D point."); 
    } 
} 

打印出来

private static void PrintMap(int size = 20) { 
    // initial empty map; I prefer using Linq for that, 
    // you can well rewrite it with good old for loops 
    char[][] map = Enumerable.Range(0, size) 
    .Select(i => Enumerable.Range(0, size) 
     .Select(j => '#') 
     .ToArray()) 
    .ToArray(); 

    // fill map with points; 
    // please, notice that we must not print points like (-1;3) or (4;100) 
    foreach (var point in s_Points) 
    if (point.Item1 >= 0 && point.Item1 < map.Length && 
     point.Item2 >= 0 && point.Item2 < map[point.Item1].Length) 
     map[point.Item1][point.Item2] = '*'; 

    // The string we want to output; 
    // once again, I prefer Linq solution, but for loop are nice as well 
    string report = string.Join(Environment.NewLine, map 
    .Select(line => string.Concat(line))); 

    Console.Write(report); 
} 

最后,所有你需要做的是调用的方法:

static void Main(string[] args) { 
    UserInput(); 
    PrintMap(); 

    Console.ReadKey(); 
} 
+0

非常感谢你的详细解释,你似乎破坏了这个C#开发:D –

+0

@BenjaminBajić:我试图提出一个现实世界的解决方案:如果我的老板要求我开发常规。 Vague和* incomplete *(坐标是如何分开的:按','或按';'或按空格...如果提供'(-1,-1)'点......)以及反复和*不稳定*(我说'20x20'?Nonsence!'45x45';打印在控制台上?废话!给我发一封电子邮件!)规格是很常见的。用户的输入是另一个担忧:“我已经试过把'1; 2'点一小时,请帮助我!” –