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);
//}
}
}
}
#연습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개");
//}
}
}
}
#연습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);
}
}
}