C#/수업 내용

여러타입의 배열 연습

Game Client Lee Hwanguk 2023. 1. 9. 11:14

#연습 1(클래스안의 값을 이용하기)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study07
{
    internal class App
    {
        public App()
        {
            //생성자
            Console.WriteLine("App생성자");
            student student1 = new student("학생1", 001, 80); ;
            student student2 = new student("학생2",002,45);
            student student3 = new student("학생3",003,70);
            student student4 = new student("학생4",004,87);
            student student5 = new student("학생5",005,82);

            student[] Students = {student1,student2,student3,student4,student5};

            
            for (int i = 0; i < Students.Length; i++)
            {
                Console.WriteLine("이름:{0}, 학번:{1}, 점수;{2}",Students[i].name, Students[i].num, Students[i].score);
            }

            //foreach(student Student in Students)
            //{
            //    Console.WriteLine("이름 :{0}, 학번:{1}, 점수:{2}", Students);
            //}



        }
    }
}

1.Student 의 클래스를 만들고 name,num,score 의 속성을 부여했다
2.매게변수를 통해 속성에 값을 부여했다
3.배열을 만들어 생성된 인스턴스 5개를 요소로 할당했다
4.for문을 이용하여 출력. (배열요소로 할당된 Studen 인스턴스들을 출력. Students[i].name
4-1 foreach 를 활용하여 출력도 가능(Studens.name값 출력)

#연습2(요소의 갯수 세기)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study07
{
    internal class App
    {
        public App()
        {
            //생성자
            Console.WriteLine("App생성자");

            int[] nums = {1,3,1,3,2,3,1,4,4,5 };

            Console.WriteLine("찾고싶은 값을 입력하세요 (1~5)");
            int input=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("{0}의 갯수는", input);

            //if(input==1)
            //{
            //    Console.WriteLine("3개");
            //}

            //else if(input==2)
            //{
            //    Console.WriteLine("1개");
            //}
            //else if(input==3)
            //{
            //    Console.WriteLine("3개");
            //}
            //else if(input==4)
            //{
            //    Console.WriteLine("2개");
            //}
            //else if(input==5)
            //{
            //    Console.WriteLine("1개");
            //}



        }
    }
}

1.하나하나 다 세보는 if문을 만들어봤다 이건 내가 세는게 더 빠르겠다 for문을 활용해보자
2.입력받은 input의 값이 nums배열의 i번째 인덱스와 같다면 j를 1 더한다. *출력을 for문 안에 넣으니 계속 출력하는 문제가 생겼다
3.밖으로 빼면 해결된다

#연습3(최대값을 구해보자)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study07
{
    internal class App
    {
        public App()
        {
            //생성자
            Console.WriteLine("App생성자");

            //최대값 구하기 
            int[] arr = { 20, 10, 35, 30, 7 };
            int temp = arr[0];
            for (int i = 0; i < arr.Length; i++)
            {
                if (temp < arr[i])
                    temp = arr[i];
            }
            Console.WriteLine(temp);

            int max=arr.Max();
            Console.WriteLine(max);


        }
    }
}

 

# .Max();를 통해 최대값을 구할수있다