プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第10章(配列)の10.9(練習問題)10-3, 4を解いてみる。
練習 10-3
using System; public class Dog { private int weight; private string name; public Dog(int weight, string name) { this.weight = weight; this.name = name; } public int Weight { get { return weight; } set { weight = value; } } public string Name { get { return name; } set { name = value; } } } class Tester { public void Run() { const int rows = 3; Dog milo = new Dog(26, "Milo"); Dog frisky = new Dog(10, "Frisky"); Dog laika = new Dog(50, "Laika"); Dog[] dogs = new Dog[3] { milo, frisky, laika }; string[][] awards = new string[rows][]; awards[0] = new string[1]; awards[1] = new string[2]; awards[2] = new string[3]; awards[0][0] = "Award"; awards[1][0] = "Award1"; awards[1][1] = "Award2"; awards[2][0] = "Award3"; awards[2][1] = "Award4"; awards[2][2] = "Award5"; for (int i = 0; i < dogs.Length; i++) { Console.WriteLine( "Weight: {0}p Name: {1}", dogs[i].Weight, dogs[i].Name); for (int j = 0; j < awards[i].Length; j++) { Console.WriteLine(awards[i][j]); } Console.WriteLine(); } } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
Weight: 26p Name: Milo Award Weight: 10p Name: Frisky Award1 Award2 Weight: 50p Name: Laika Award3 Award4 Award5 続行するには何かキーを押してください . . .
練習 10-4
using System; class Tester { public void Run() { const int rows = 8; const int columns = 8; string[,] chessboard = new string[rows, columns]; for (int i = 0; i < rows; i++) { if ((i % 2) == 0) { for (int j = 0; j < columns; j++) { if ((j % 2) == 0) { chessboard[i, j] = "黒"; } else { chessboard[i, j] = "白"; } } } else { for (int j = 0; j < columns; j++) { if ((j % 2) == 0) { chessboard[i, j] = "白"; } else { chessboard[i, j] = "黒"; } } } } Console.WriteLine("チェス盤の座標の色を求めます"); Console.Write("行を入力してください>>"); string row = Console.ReadLine(); Console.Write("列を入力してください>>"); string column = Console.ReadLine(); int m = Convert.ToInt32(row); int n = Convert.ToInt32(column); Console.WriteLine( "チェス盤{0}行{1}列目の色は{2}です", m, n, chessboard[m - 1, n - 1]); } static void Main() { Tester t = new Tester(); t.Run(); } }
チェス盤の座標の色を求めます 行を入力してください>>5 列を入力してください>>6 チェス盤5行6列目の色は白です 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿