반응형
SMALL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study07
{
class Students
{
string[] names = { "김동준", "이환국", "홍길동", "재갈량", "조조" };
int[] ids = { 20230109, 20230110, 20230111, 20230112, 20230113 };
int[] scores = { 100, 90, 56, 75, 11 };
//생성자
public Students()
{
}
public void StudentsList()
{
foreach (string name in names)
{
Console.Write(name);
}
foreach (int id in ids)
{
Console.Write(id);
}
foreach (int score in scores)
{
Console.Write(score);
}
}
}
}
문제점 : 학생"들' 이 데이터를 가지고 있다
내가 풀은 방법
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study07
{
class App
{
//생성자
public App()
{
Student student0 = new Student();
student0.id = 20230109;
student0.name = "홍길동";
student0.score = 89;
Student student1 = new Student();
student1.id = 20230109;
student1.name = "임꺽정";
student1.score = 89;
Student student2 = new Student();
student2.id = 20230109;
student2.name = "장길산";
student2.score = 89;
Student[] students = {student0, student1, student2};
foreach (Student student in students)
{
Console.WriteLine("학번 : {0}",student.id);
Console.WriteLine("학생이름 : {0}", student.name);
Console.WriteLine("점수 : {0}", student.score);
Console.WriteLine();
}
}
}
}
for 문 추가
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study07
{
class App
{
//생성자
public App()
{
Student student0 = new Student();
student0.id = 20230109;
student0.name = "홍길동";
student0.score = 89;
Student student1 = new Student();
student1.id = 20230109;
student1.name = "임꺽정";
student1.score = 89;
Student student2 = new Student();
student2.id = 20230109;
student2.name = "장길산";
student2.score = 89;
Student[] students = {student0, student1, student2,null,null};
//foreach (Student student in students)
//{
// if (student != null)
// {
// Console.WriteLine("학번 : {0}", student.id);
// Console.WriteLine("학생이름 : {0}", student.name);
// Console.WriteLine("점수: {0}",student.score);
// Console.WriteLine();
// }
// else
// return;
//}
for (int i = 0; i <= students.Length; i++)
{
if (students[i] != null)
{
Console.WriteLine("학번 : {0}", students[i].id);
Console.WriteLine("학생이름 : {0}", students[i].name);
Console.WriteLine("점수 : {0}", students[i].score);
}
else
return;
}
}
}
}
반응형
LIST
'C# > 문제 해결' 카테고리의 다른 글
외부 클래스 3개를 이용한 람다 Action<> (0) | 2023.01.12 |
---|---|
배열 안의 최대값 구하기 (0) | 2023.01.09 |
class 연습 (0) | 2023.01.04 |
고블릭 죽이기 (계속 체력이 -로 가는 문제) (0) | 2023.01.03 |