演習問題を始める前に
こんにちは、かっぱちゃんです。今回の記事は初級編の練習問題6です。
前回の演習問題は下記のリンクです、まだ解いていない方はこちらの問題から解いてみてください。
こちらもCHECK
-
-
C# Linq演習問題 ~Linqマスターへの道~ 初級編5
演習問題を始める前にContents1 演習問題を始める前に1.1 演習問題を解く前に下記のソースコードをコピーして貼り付け1.2 第1問1.3 第2問1.4 第3問1.5 第4問1.6 第5問1.7 ...
続きを見る
演習問題をまとめてあります。
他の演習問題はこちら
今回は一つ別のクラスを使用した演習問題を載せていきますので、クラスをコピーしてから初めて下さい。
今までの復習がメインなので、どんどん解いて下さい。

-
-
合わせて読みたいC# Linqを勉強するならこれだけは読んでほしいオススメの本3選 オススメの参考書
C#を勉強するためのオススメの本を紹介Contents1 C#を勉強するためのオススメの本を紹介1.1 C#の基礎を学ぶなら「やさしいC#」1.2 C#の全体像を学ぶなら「独習C#」1.3 C#をさら ...
続きを見る
演習問題を解く前に下記のソースコードをコピーして貼り付け
下記のソースコードを貼り付けてください。Program.csファイルでもRobot.csファイルを作成して貼り付けても問題ありません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//下記のロボットクラスをコピーして、貼り付けて下さい。 class Robot { string name, maker, color; int series,strength; public Robot(string _name,string _maker,string _color,int _series,int _strength) { name = _name; maker = _maker; color = _color; series = _series; strength = _strength; } public string getName() { return name; } public string getMaker() { return maker; } public string getColor() { return color; } public int getSeries() { return series; } public int getStrength() { return strength; } } |

第1問
ロボット名をシリーズ順、強い順に並び替えてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); //**************ここからコードを書いて下さい************** var robotslists = //**************ここまでコードを書いて下さい************** foreach (var rlist in robotslists) { Console.WriteLine("Series:" + rlist.getSeries() + " Strength:" + rlist.getStrength() + " Name:" + rlist.getName()); } } } |
出力結果
Series:1 Strength:5 Name:Rockman
Series:1 Strength:4 Name:Iceman
Series:1 Strength:3 Name:Fireman
Series:1 Strength:1 Name:Rollchan
Series:2 Strength:5 Name:Quickman
Series:2 Strength:3 Name:Woodman
Series:2 Strength:2 Name:Heatman
Series:3 Strength:5 Name:Shadowman
Series:3 Strength:4 Name:Sparkman
Series:3 Strength:3 Name:Snakeman

第2問
シリーズが2の作品で、強さが4以上のロボット名のみ出力してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); //**************ここからコードを書いて下さい************** var robotslists = //**************ここまでコードを書いて下さい************** foreach (var rlist in robotslists) { Console.WriteLine("Name:" + rlist); } } } |
出力結果
Name:Quickman
第3問
三作品目のロボット名を強さが弱い順に並べてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); //**************ここからコードを書いて下さい************** var robotslists = //**************ここまでコードを書いて下さい************** foreach (var rlist in robotslists) { Console.WriteLine("Name:" + rlist); } } } |
出力結果
Name:Snakeman
Name:Sparkman
Name:Shadowman
第4問
ロボット名がSから始まるロボット名を強い順に並べてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); //**************ここからコードを書いて下さい************** var robotslists = //**************ここまでコードを書いて下さい************** foreach (var rlist in robotslists) { Console.WriteLine("Name:" + rlist); } } } |
出力結果
Name:Shadowman
Name:Sparkman
Name:Snakeman
第5問
ロボットリストから製造者がワイリーかつ青いロボット以外のロボット名を出力してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); //**************ここからコードを書いて下さい************** var robotslists = //**************ここまでコードを書いて下さい************** foreach (var rlist in robotslists) { Console.WriteLine("Name:" + rlist); } } } |
出力結果
Name:Heatman
Name:Fireman
Name:Quickman
Name:Snakeman
Name:Woodman
Name:Sparkman

まとめ
今回の問題はクラスを利用したLinqメソッドの使い方の練習問題でした。
おめでとうございます。ここまで難なく問題が解けるようになった方はLinqメソッドの基本的な使い方は完璧です。
既に何個か記事を書いていますが、中級クラスの問題も解けると思います。中級以上の記事も沢山のせていくので、どんどん解いて下さいね。

他の演習問題はこちら