코딩 공부/Unity C#

유니티 1주차 과제

recordmastd 2024. 5. 30. 00:36

과제 설명

여러분은 화학이 탐구하기에 멋진 분야라고 결정했습니다. 최근 멘델레예프의 원소 주기율표 150주년을 기념하여 (현재) 원소 주기율표를 살펴보기로 결정했습니다. 그 배열의 아름다움과 우아함에 너무 흥분하여 원소 주기율표의 처음 10개 원소에 대한 기호를 인쇄하기로 결정합니다.

 

요구 사항

솔루션은 다음을 수행해야 합니다:

  • 주기율표의 처음 10개 원소 각각에 대한 기호를 출력에서 각 기호가 고유한 줄에 표시되도록 인쇄합니다
  • 출력물에 다른 기호를 인쇄하지 마십시오. 인쇄하면 자동 채점기를 혼동하여 잘못된 성적을 받을 수 있습니다!

출력의 모양을 이해하는 데 어려움이 있는 경우, 다음은 필수 출력의 처음 3줄입니다(출력은 10줄이 됩니다):

H

He

Li

중요 참고: Coursera 서식으로 인해 위의 각 줄 사이에 빈 줄이 있는 것처럼 보이지만 실제로는 없습니다. 위의 예제 출력은 정확히 3줄의 출력입니다.

코딩 실행하기

Coursera에서 자동 채점기로 작업하기 위해 포함시킨 코드 때문에 프로그램을 실행하면 명령 프롬프트 창이 열리고 아무 작업도 하지 않습니다. 코딩을 실행하려면 1을 입력하고 <Enter> 키를 누르면 코드가 실행되어 출력을 확인할 수 있습니다.

원한다면 1을 입력하고 <Enter> 키를 다시 눌러 코딩을 다시 실행할 수 있습니다. 코딩 실행을 중지할 준비가 되면 종료의 경우 q를 입력합니다.

 

 

<처음 작성한 답안> (오답. 0/100, 출력이 하나만 나옴)

using System;
using System.Diagnostics;

namespace ProgrammingAssignment1
{
    // IMPORTANT: Only add code in the section
    // indicated below. The code I've provided
    // makes your solution work with the 
    // automated grader on Coursera

    /// <summary>
    /// Programming Assignment 1
    /// </summary>
    class Program
    {
        /// <summary>
        /// Programming Assignment 1
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // loop while there's more input
            string input = Console.ReadLine();    // 변수 input 초기화
            while (input[0] != 'q')
            {
                // Add your code between this comment
                // and the comment below. You can of
                // course add more space between the
                // comments as needed
                Console.WriteLine(input);

                // Don't add or modify any code below
                // this comment
                input = Console.ReadLine();
            }
        }
    }
}

처음에는 문제를 이해하지 못했음... 

Console.WriteLine을 C++의 cin처럼 이해해서 사용자의 입력이 필요한 줄 알았다. 

그래서 string input = Console.ReadLine(); 에서 사용자가 원소 기호를 입력하고 Console.WriteLine(input);에서 출력, input = Console.ReadLine(); 으로 그 다음 번호 원소 기호를 입력하는 구조로 착각함.

list형을 만들어야 하나 고민했지만 지금 단계에서 나올리가 없는 내용이라고 판단했음.

 

- Consosle.WriteLine()으로도 변수 초기화가 가능함

 

<정답 코드>

using System;
using System.Diagnostics;

namespace ProgrammingAssignment1
{
    // IMPORTANT: Only add code in the section
    // indicated below. The code I've provided
    // makes your solution work with the 
    // automated grader on Coursera

    /// <summary>
    /// Programming Assignment 1
    /// </summary>
    class Program
    {
        /// <summary>
        /// Programming Assignment 1
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // loop while there's more input
            string input = Console.ReadLine();
            while (input[0] != 'q')
            {
                // Add your code between this comment
                // and the comment below. You can of
                // course add more space between the
                // comments as needed
                Console.WriteLine("H");
                Console.WriteLine("He");
                Console.WriteLine("Li");
                Console.WriteLine("Be");
                Console.WriteLine("B");
                Console.WriteLine("C");
                Console.WriteLine("N");
                Console.WriteLine("O");
                Console.WriteLine("F");
                Console.WriteLine("Ne");
                // Don't add or modify any code below
                // this comment
                input = Console.ReadLine();		// q(끝) 또는 1(출력)을 입력받음
            }
        }
    }
}

그냥 원소 기호 10개 다 wrtieLine해서 q를 누르기 전까지 1을 누를 때마다 한 세트로 출력되도록 하는 거였다.

코드를 보면 알겠지만 꼭 1을 눌러야 출력이 나오는 건 아니고 q만 입력하지 않으면 원소 기호 10개가 출력된다.

 

루프문 밖에 있는 string input = Console.ReadLine();은 변수 input의 초기화를 위해 사용되었고,

루프문 안에 있는 input = Console.ReadLine(); 는 1 또는 q 입력을 받기 위해 사용