プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第10章(配列)の10.9(練習問題)を解いてみる。
練習 10-1
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() { Dog milo = new Dog(26, "Milo"); Dog frisky = new Dog(10, "Frisky"); Dog laika = new Dog(50, "Laika"); Dog[] dogs = { milo, frisky, laika }; Console.WriteLine("Dog"); foreach (Dog dog in dogs) { Console.WriteLine( "Name: {0} Weight: {1}", dog.Name, dog.Weight); } } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
Dog Name: Milo Weight: 26 Name: Frisky Weight: 10 Name: Laika Weight: 50 続行するには何かキーを押してください . . .
練習 10-2
using System; class Tester { public static void PrintArray(int[] intArray) { foreach (int i in intArray) { Console.WriteLine("値: {0}", i); } } public void Run() { int[] intArray = new int[10]; Console.WriteLine( "以下整数を10個入力してください"); for (int i = 0; i < intArray.Length; i++) { Console.Write("{0}個目>>", i + 1); string s = Console.ReadLine(); intArray[i] = Convert.ToInt32(s); } Console.WriteLine("\n配列"); PrintArray(intArray); // 配列をソート Array.Sort(intArray); // 配列の順序を逆にする Array.Reverse(intArray); Console.WriteLine( "\n最大値から最小値にソート後の配列"); PrintArray(intArray); } static void Main() { Tester t = new Tester(); t.Run(); } }
以下整数を10個入力してください 1個目>>1 2個目>>3 3個目>>2 4個目>>4 5個目>>0 6個目>>9 7個目>>7 8個目>>8 9個目>>6 10個目>>5 配列 値: 1 値: 3 値: 2 値: 4 値: 0 値: 9 値: 7 値: 8 値: 6 値: 5 最大値から最小値にソート後の配列 値: 9 値: 8 値: 7 値: 6 値: 5 値: 4 値: 3 値: 2 値: 1 値: 0 続行するには何かキーを押してください . . .
練習 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 = { milo, frisky, laika }; string[][] dogsAwards = new string[rows][]; dogsAwards[0] = new string[1]; dogsAwards[1] = new string[3]; dogsAwards[2] = new string[2]; dogsAwards[0][0] = "Award1"; dogsAwards[1][0] = "Award2"; dogsAwards[1][1] = "Award3"; dogsAwards[1][2] = "Award4"; dogsAwards[2][0] = "Award5"; dogsAwards[2][1] = "Award6"; Console.WriteLine("Dog"); for (int i = 0; i < dogsAwards.Length; i++) { Console.WriteLine( "\n{0}がドッグショーで獲得した賞", dogs[i].Name); for (int j = 0; j < dogsAwards[i].Length; j++) { Console.WriteLine(dogsAwards[i][j]); } } } static void Main() { Tester t = new Tester(); t.Run(); } }
Dog Miloがドッグショーで獲得した賞 Award1 Friskyがドッグショーで獲得した賞 Award2 Award3 Award4 Laikaがドッグショーで獲得した賞 Award5 Award6 続行するには何かキーを押してください . . .
練習 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( "行, 列番号から8*8のchessboardのマス目の色を求めます"); Console.Write("行番号を入力してください>>"); string rowNum = Console.ReadLine(); int row = Convert.ToInt32(rowNum); Console.Write("列番号を入力してください>>"); string colNum = Console.ReadLine(); int col = Convert.ToInt32(colNum); Console.WriteLine("{0}行{1}列目のマス目の色は{2}です", row, col, chessboard[(row - 1), (col - 1)]); } static void Main() { Tester t = new Tester(); t.Run(); } }
行, 列番号から8*8のchessboardのマス目の色を求めます 行番号を入力してください>>4 列番号を入力してください>>5 4行5列目のマス目の色は白です 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿