2016-11-11 4 views
0

私は数日前に受け取った仕事をしようとしています。基本的にタスクはC#のコンソールアプリケーションです。c#2次元配列で入力座標を使用して「*」文字を印刷

"stop"という単語が入力されるまで、2つの座標を入力するようにユーザーに指示します。 "stop"という単語がヒットしたら、各入力座標に "*"(星印)を表示します。座標が印刷されるフィールドは20x20です。私はこれをやってみたが、役に立たなかった。誰かが私を助けて、どのように入力xを格納するには、yの2次元配列に私を見ることができれば、それは:)

は、アプリケーションが動作するはずですどのように素晴らしいことだ:[0,5] http://imgur.com/a/SnC1k

[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("*"); 
      } 






     } 


    } 
    } 
    } 

コードは、私はちょうどそれをしていませんでした。私はそれを行うことを試みたが、うまくいきませんでしたどのように

。それは私が修正する方法がわからない複数のエラーを示しています。私ができる唯一のことは、入力されたすべての座標に文字を印刷するのではなく、最初の座標を入力するとすぐに文字を印刷することです。ヒットしました。

+0

あなたの問題の多くを解決するかもしれない何かは、私はあなたと思うwhile(!' –

+1

あなたもレスポンスを小文字に変換していますが、「停止」ではなく「停止」への応答を比較しています –

+0

これらの間違いを指摘してくれてありがとう:) –

答えて

0

また、あなたが何らかの形でユーザーの入力をお読みくださいとストアの代わりに指定された位置のために適切な値を設定しConsole.SetCursorPosition(x, y);

を使用する必要はありません座標。

これを試して、これがどのように機能するか教えてください。 thisフィドルでもご覧になれます。座標をスペースで区切った2つの数字として入力し、停止して印刷します。

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で書くときAlthoそれはちょうどインデックスが配列から外れていると言ってクラッシュする私はそれがないと思う? –

+0

Yeap、あなた自身の必要に応じて検証する必要があります。 C#の配列はzoreベースなので、この場合20,15は最後の位置が19,19と無効な位置です –

+0

ありがとう、あなたのコーディングスキルは本当にうらやましいレベルです。 :) –

1

20×20の2次元配列を作成します。ユーザーにxとyを入力するよう促します。いったん各配列を配列[x、y]に1つ格納すれば、ユーザーは配列の中でstopループを打ち、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

私はを分解することを奨励します。この作業は、悪い習慣として1つの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

詳細な説明をお寄せいただきありがとうございます。D –

+0

@BenjaminBajić:現実の世界の解決策を試しました。私の上司が私に開発を依頼したらどうすればいいでしょうか?ルーチン。 (-1、-1)ポイントが提供されていればどうなるでしょうか...)とfickleと*不安定な*(私は '20x20'と言っていましたか?いいえ!45x45';コンソールでプリントアウトするか、私のメールを送ってください!)仕様はかなり一般的です。ユーザー入力はもう一つの懸念事項です。「1時間2分のポイントを入れてみてください。私を助けてください!」 –

関連する問題