배열&리스트
int[] data = { 5, 12, 3, 8, 10 };
for (int i = 0; i < data.Length; i++)
{
if (data[i] > 10)
{
Console.WriteLine(data[i]);
}
}
for (int i = 0; i < data.Length; i++)
{
if (data[i] % 2 == 0)
{
Console.WriteLine(data[i]);
}
}
int sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += data[i];
}
Console.WriteLine($"총 합 : {sum}");
foreach (int num in data)
{
if (num > 5)
{
Console.WriteLine(num);
}
}
->int data에 값 할당.data,Length(길이)만큼 1씩 증가. 1번째는 10보다 클때 출력. 2번째는 2로 나누었을때 나머지가 0이면 출력(짝수).
3번째는데이터의 총 합 출력.
4번째는 foreach문을 이용해 5보다 크면 전부 출력.
클래스문
public class Player
{
//필드 (변수 선언)
public string name;
public int hp;
//생성자. 외부에서 전달된 이름 저장
public Player(string name, int hp)
{
this.name = name;
this.hp = hp;
}
//외부에서 호출 가능
public void Attack()
{
Console.WriteLine($"{name}이(가) 공을 찹니다!");
}
//데미지를 받는 메서드. hp에 -데미지를 넣어서 계산.
public void TakeDamage(int damage)
{
hp -= damage;
Console.WriteLine($"{name}이(가) {damage} 데미지를 입음. 남은 HP: {hp}");
}
}
-> public으로 접근 허용. this로 초기화 진행. $인 문자 보관열로 작성. 메인에서 호출하면 끝