본문 바로가기

정리 카테고리/실습_과제

실습_배열로 과목별 성적 받고 성적 및 합계,평균 출력

1.요구 사항

-배열 사용

-성적 3가지 받기

-성적 출력

-합점 및 평균


2.결과 화면 및 샘플

배열_성적_합계_평균.exe



3.소스 코드




 
            int[] scores = new int[9];
            string[] names = new string[3] { "림꺽정", "롤길동", "림정일" };
            string[] subjects = new string[3] { "국어", "수학","과학" };


            for(int i = 0; i < names.Length; i++)
            {
                Console.Clear();
                for(int j = 0; j < subjects.Length; j++)
                {
                    Console.WriteLine(names[i] + "의" + subjects[j] + " 성적을 입력해 주세요.");
                    Console.Write("::");
                    int parse = 0;
                    if (int.TryParse(Console.ReadLine(), out parse)) scores[i * 3 + j] = parse;
                    else
                    {
                        Console.WriteLine("정상적인 성적을 입력해 주세요.");
                        j--;
                    }
                }


            }
            Console.Clear();
            for (int i = 0; i < names.Length; i++)
            {
                int total = 0;

                for (int j = 0; j < subjects.Length; j++)
                {
                   
                    Console.WriteLine(names[i] + "의" + subjects[j] + " 성적은 "+scores[i*3+j]);
                    total += scores[i * 3 + j];
                    if (j == (subjects.Length - 1))
                    {
                        Console.WriteLine(names[i] + "의 총 합점은 " + total);
                        Console.WriteLine(names[i] + "의 총 합점은 " + (total/subjects.Length));
                    }
                }
                Console.WriteLine("\n");

            }

            Console.ReadKey();