using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class Item
{
public string name;
//생성자
public Item(string name)
{
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class App
{
//생성자
public App()
{
Inventory inventory = new Inventory();
inventory.AddItem(new Item("장검")); //AddItem은 배열 items
inventory.AddItem(new Item("단검"));
inventory.PrintItems();
inventory.FindItem("장검");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class Inventory
{
Item[] items;
int index = 0;
//생성자
public Inventory()
{
this.items = new Item[5];
}
public void AddItem(Item item)
{
//배열(items)에 매개변수로 전달받은 Item을 저장
//배열의 요소에 접근해서 값을 할당하려면
//인덱스을 알아야 한다
this.items[index] = item;
index++; //다음 배열의 요소
//아이템을 배열에 넣을때마다
//인덱스의 값을 1 증가 시키자 (배열의 다음요소)
}
public void PrintItems() //items에 들어가있는 item들을 확인해보자
{
//for문 사용
index = 0;
for (int i = 0; i < items.Length; i++)
{
if (items[i] != null)
{
Console.WriteLine("{0}", items[index].name);
}
else
{
Console.WriteLine("비어있음");
}
index++;
}
}
public void FindItem(string name)
{
//매게변수로 들어온 문자열 값을 통해 인덱스를 찾아보자
index = 0;
for (int j = 0; j < items.Length; j++)
{
if (items[j] != null && items[j].name == name)
{
Console.WriteLine("{0} 있음", items[j].name);
}
index++;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class App
{
//생성자
public App()
{
//int[] scores = new int[5];
int[] scores = { -1, -1, -1, -1, -1 };// new int[5];
//scores[0] = -1;
//scores[1] = -1;
//scores[2] = -1;
//scores[3] = -1;
//scores[4] = -1;
Console.WriteLine("length: {0}", scores.Length);
Console.WriteLine("start index: {0}, last index : {1}", 0, scores.Length - 1);
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine("index {0} value : {1}", i, scores[i]);
}
scores[0] = 80;
scores[1] = 74;
scores[2] = 81;
scores[3] = 90;
scores[4] = 43;
int index = 0;
index++;
scores[index] = 82;
int j = 0;
int sum = 0;
foreach (int score in scores)
{
Console.WriteLine("index: {0}, value: {1}", j, score);
sum = sum + score;
j++;
}
Console.WriteLine("sum: {0}", sum);
}
}
}
<학생세기>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class App
{
//생성자
public App()
{
Student student0 = new Student();
student0.id = 100;
student0.name = "홍길동";
student0.score = 89;
Student student1 = new Student();
student1.id = 101;
student1.name = "임꺽정";
student1.score = 92;
Student student2 = new Student();
student2.id = 102;
student2.name = "장길산";
student2.score = 78;
Student[] students = { student0, student1, student2, null, null };
//for, foreach를 사용해 배열의 모든 요소(학생 데이터만)들의 정보를 출력
//100 홍길동 89
//101 임꺽정 92
//102 장길산 78
for(int i=0; i<students.Length; i++)
{
if(students[i]!=null)
Console.WriteLine("{0} {1} {2}", students[i].id, students[i].name, students[i].score);
}
foreach(Student student in students)
{
if (student != null)
Console.WriteLine("{0} {1} {2}", student.id, student.name, student.score);
}
}
}
}
<개수세기>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class App
{
//생성자
public App()
{
//개수세기(해당 지정 숫자 세기)
int[] arr = { 1, 3, 1, 3, 2, 3, 1, 4, 4, 5 };
int n = Convert.ToInt32( Console.ReadLine()); //if 조건식에서 형식을 맞춰야 하므로 int n으로 설정
Console.WriteLine("input : {0}", n);
int count = 0;
for(int i = 0; i<arr.Length; i++)
{
if (n == arr[i]) //입력받은 값이 배열에 값과 같다면
count++;
}
Console.WriteLine("count : {0}", count);
}
}
}
<최댓값 구하기>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study0121
{
class App
{
//생성자
public 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);
}
}
}
'c# > 복습' 카테고리의 다른 글
(재연습)컬렉션 (0) | 2023.01.24 |
---|---|
(재연습)2차원 배열 (0) | 2023.01.22 |
(재연습) switch, while (0) | 2023.01.21 |
(재연습) 시즈탱크(탱크모드/공성모드) (0) | 2023.01.21 |
(재연습)클래스 연습 (0) | 2023.01.21 |