Grading Logic
Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨 — 66개 중 43번째.
챌린지
쉬움이제 데이터를 분석할 수 있으니, 점수를 기반으로 문자 등급을 부여하는 등급 시스템을 구현해 보겠습니다. GradingSystem이라는 클래스에 다음 메서드들을 만드세요:
ConvertToLetterGrade(double score): 숫자 점수를 이 척도에 따라 문자 등급으로 변환합니다:- A: 90-100
- B: 80-89
- C: 70-79
- D: 60-69
- F: 0-59
- 유효하지 않음: 유효하지 않은 점수(음수 또는 > 100)에 대해 "N/A"를 반환하세요
GetStudentGrade(int[][] scoreGrid, int studentIndex): 학생의 평균을 계산하고 문자 등급을 반환합니다. 유효하지 않은 학생 인덱스에 대해 "N/A"를 반환하세요.GetClassDistribution(int[][] scoreGrid): 클래스 내 각 문자 등급의 개수를 나타내는 정수 배열 [A, B, C, D, F]을 반환합니다. 각 학생의 평균 점수를 기반으로 합니다.
직접 해보기
using System; // 이 줄을 삭제하지 마세요
public class GradingSystem
{
public static string ConvertToLetterGrade(double score)
{
// 여기에 코드를 작성하세요
}
public static string GetStudentGrade(int[][] scoreGrid, int studentIndex)
{
// 여기에 코드를 작성하세요
}
public static int[] GetClassDistribution(int[][] scoreGrid)
{
// 여기에 코드를 작성하세요
}
}
// Required for testing - do not modify
public class DataCollector
{
public static int[][] CreateScoreGrid(int students, int assignments)
{
int[][] scoreGrid = new int[students][];
for (int i = 0; i < students; i++)
{
scoreGrid[i] = new int[assignments];
}
return scoreGrid;
}
public static bool ValidateScore(int score)
{
return score >= 0 && score <= 100;
}
public static int[][] PopulateWithDefaultValues(int[][] scoreGrid)
{
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
scoreGrid[i][j] = -1;
}
}
return scoreGrid;
}
}
public class DataEntry
{
public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
{
// 인덱스가 범위를 벗어났는지 확인
if (studentIndex < 0 || studentIndex >= scoreGrid.Length ||
assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
{
return -1;
}
// 점수를 검증
if (!DataCollector.ValidateScore(score))
{
return -2;
}
// 점수를 설정
scoreGrid[studentIndex][assignmentIndex] = score;
return 0;
}
public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
{
int successCount = 0;
for (int i = 0; i < studentIndices.Length; i++)
{
int result = SetStudentScore(scoreGrid, studentIndices[i], assignmentIndex, score);
if (result == 0)
{
successCount++;
}
}
return successCount;
}
}
public class DataAnalyzer
{
public static double CalculateStudentAverage(int[][] scoreGrid, int studentIndex)
{
// 학생 인덱스가 유효한지 확인
if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
{
return -1;
}
int sum = 0;
int count = 0;
// 유효한 점수들의 합을 계산
for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
{
int score = scoreGrid[studentIndex][j];
if (score != -1) // 미채점 과제 무시
{
sum += score;
count++;
}
}
// 유효한 점수가 없으면 평균 또는 0을 반환
return count > 0 ? (double)sum / count : 0;
}
public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
{
// 학생이 있는지 확인
if (scoreGrid.Length == 0)
{
return -1;
}
// 과제 인덱스가 유효한지 확인
if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
{
return -1;
}
int sum = 0;
int count = 0;
// 해당 과제의 유효한 점수들의 합을 계산
for (int i = 0; i < scoreGrid.Length; i++)
{
if (assignmentIndex < scoreGrid[i].Length)
{
int score = scoreGrid[i][assignmentIndex];
if (score != -1) // 미채점 과제 무시
{
sum += score;
count++;
}
}
}
// 유효한 점수가 없으면 평균 또는 0을 반환
return count > 0 ? (double)sum / count : 0;
}
public static int[] FindHighestScore(int[][] scoreGrid)
{
int highestStudentIndex = 0;
int highestAssignmentIndex = 0;
int highestScore = -1;
// 최고 점수를 검색
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
int currentScore = scoreGrid[i][j];
if (currentScore > highestScore)
{
highestScore = currentScore;
highestStudentIndex = i;
highestAssignmentIndex = j;
}
}
}
return new int[] { highestStudentIndex, highestAssignmentIndex, highestScore };
}
}논리 및 흐름의 모든 레슨
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling8Data Analysis System
Data Collection SetupData Entry Logic3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety