• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

IEnumerable 강의 따라하는 중 질문합니다.

23.07.13 17:43 작성 23.07.13 17:43 수정 조회수 270

0

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Exam11_1

{

class Exam11_1

{

public void Run()

{

int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

IEnumerator enumerator = intArray.GetEnumerator();

for (int i = 0; i < intArray.Length; i++) Console.WriteLine(intArray[i]);

}

}

}

도저히 아무리 봐도 저는 왜 아무것도 안 나오는지 모르겠습니다.ㅠㅠ

답변 2

·

답변을 작성해보세요.

0

dev zipida님의 프로필

dev zipida

질문자

2023.07.18

강의 21분 35초 보고 더 따라했는데도 똑같습니다.ㅠㅠ

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Exam11_1

{

class Exam11_1

{

public void Run()

{

TestBasic();

//TestStudent();

}

private void TestStudent()

{

Student[] students = new Student[]

{new Student("홍길동", 89, 1), new Student("황진이", 87, 2)};

IEnumerator enumerator = students.GetEnumerator();

while (enumerator.MoveNext()) Console.WriteLine(enumerator.Current);

}

private void TestBasic()

{

int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//for (int i = 0; i < intArray.Length; i++) Console.WriteLine(intArray[i]);

//IEnumerator enumerator = intArray.GetEnumerator();

//while (enumerator.MoveNext())

//{

// Console.WriteLine(enumerator.Current);

//}

//foreach()

//foreach (int i in intarray) Console.WriteLine(i);

string str = "abcdefg";

foreach (char c in str) Console.WriteLine(c);

}

}

class Student

{

public string Name { get; set; }

public int Score { get; set; }

public int Id { get; set; }

public Student(string name, int score, int id)

{

Name = name;

Score = Score;

Id = id;

}

public override string ToString()

{

return $"{Name}[{Id}][{Score}]";

}

}

}

 

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.07.15

안녕하세요, 인프런 AI 인턴이에요.
제작자인 dev zipida님께서 IEnumerable 강의를 따라하시면서 출력이 되지 않는 문제가 있다고 하셨네요.

코드를 살펴보니 Run() 메소드 안에서 intArray 배열의 모든 요소를 출력하려고 하셨습니다. 하지만 현재 코드에서는 IEnumerator를 사용하여 반복문을 구성하지 않고 있어서 출력이 되지 않는 것 같습니다.

해결 방법으로는 IEnumerator 인터페이스를 사용하지 않고 foreach 루프를 사용하여 intArray 배열의 요소를 출력할 수 있습니다. 아래와 같이 코드를 수정해 보세요.

public void Run()
{
    int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    foreach (var num in intArray)
    {
        Console.WriteLine(num);
    }
}

위의 코드처럼 수정하면 intArray 배열의 모든 요소가 하나씩 출력될 것입니다. 해당 부분을 수정해보시고 원하는 결과가 나오는지 확인해보세요. 문제가 다른 부분이 있다면 알려주세요. 감사합니다.