본문 바로가기
Programming Language/이것이 C# 연습문제 풀이

이것이 C# 15장 연습문제 풀이

by dbxxrud 2019. 12. 20.

 

 

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
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.IO;
 
namespace Practice2
{
    class Car
    {
        public int Cost { get; set; }
        public int MaxSpeed { get; set; }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Car[] cars =
            {
                new Car() {Cost=56,MaxSpeed=120 },
                new Car() {Cost=70,MaxSpeed=150 },
                new Car() {Cost=45,MaxSpeed=180 },
                new Car() {Cost=32,MaxSpeed=200 },
                new Car() {Cost=82,MaxSpeed=280 }
            };
            var selected = from car in cars
                           where car.Cost >= 50 &&
                           car.MaxSpeed >= 150
                           select new
                           {
                               cost = car.Cost,
                               maxspeed = car.MaxSpeed
                            };
            foreach(var car in selected)
                WriteLine("Cost = {0}, MaxSpeed = {1}", car.cost, car.maxspeed);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

결과

 

 

 

2번 문제

 

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
38
39
40
41
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.IO;
 
namespace Practice2
{
    class Car
    {
        public int Cost { get; set; }
        public int MaxSpeed { get; set; }
    }
    class MainApp
    {
        static void Main(string[] args)
        {
            Car[] cars =
            {
                new Car() {Cost=56,MaxSpeed=120 },
                new Car() {Cost=70,MaxSpeed=150 },
                new Car() {Cost=45,MaxSpeed=180 },
                new Car() {Cost=32,MaxSpeed=200 },
                new Car() {Cost=82,MaxSpeed=280 }
            };
 
            var selected = from car in cars
                                   where car.Cost < 60
                                   orderby car.Cost
                                   select new
                                   {
                                       cost = car.Cost,
                                       maxspeed = car.MaxSpeed
                                   };
            foreach (var car in selected)
                WriteLine("Cost = {0}, MaxSpeed = {1}", car.cost, car.maxspeed);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs