開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の10章(配列)、10.9(練習問題)、問題10-4.を取り組んでみる。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Chessboard()
{
string[,] chessboard = new string[8, 8];
for (int i = 0; i < 8; i++)
{
if (i % 2 == 0)
{
for (int j = 0; j < 8; j++)
{
chessboard[i, j] = j % 2 == 0 ? "黒" : "白";
}
}
else
{
for (int j = 0; j < 8; j++)
{
chessboard[i, j] = j % 2 == 0 ? "白" : "黒";
}
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write(chessboard[i, j]);
}
Console.WriteLine();
}
while (true)
{
string s = Console.ReadLine();
if (s == "q")
{
break;
}
int row = Convert.ToInt32(s);
int col = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(chessboard[row - 1, col - 1]);
}
}
static void Main(string[] args)
{
Chessboard();
}
}
}
入出力結果(コマンドプロンプト)
黒白黒白黒白黒白 白黒白黒白黒白黒 黒白黒白黒白黒白 白黒白黒白黒白黒 黒白黒白黒白黒白 白黒白黒白黒白黒 黒白黒白黒白黒白 白黒白黒白黒白黒 1 1 黒 1 2 白 2 1 白 2 2 黒 q 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿