Grading Logic
Teil des Abschnitts Logik & Ablauf der C#-Journey von Coddy — Lektion 43 von 66.
Aufgabe
EinfachDa wir die Daten nun analysieren können, implementieren wir ein Notensystem, um Buchstabennoten basierend auf den Punktzahlen zuzuweisen. Erstellen Sie eine Klasse namens GradingSystem mit diesen Methoden:
ConvertToLetterGrade(double score): Konvertiert eine numerische Punktzahl in eine Buchstaben-Note gemäß dieser Skala:- A: 90-100
- B: 80-89
- C: 70-79
- D: 60-69
- F: 0-59
- Ungültig: Geben Sie "N/A" für jede ungültige Punktzahl zurück (negativ oder > 100)
GetStudentGrade(int[][] scoreGrid, int studentIndex): Berechnet den Durchschnitt eines Schülers und gibt seine Buchstaben-Note zurück. Geben Sie "N/A" für ungültige Schülerindizes zurück.GetClassDistribution(int[][] scoreGrid): Gibt ein Array von Ganzzahlen zurück, das die Anzahl jeder Buchstaben-Note in der Klasse darstellt [A, B, C, D, F]. Basieren Sie dies auf dem Durchschnitt der Punktzahl jedes Schülers.
Probier es selbst
using System; // Diese Zeile nicht löschen
public class GradingSystem
{
public static string ConvertToLetterGrade(double score)
{
// Schreibe deinen Code hier
}
public static string GetStudentGrade(int[][] scoreGrid, int studentIndex)
{
// Schreibe deinen Code hier
}
public static int[] GetClassDistribution(int[][] scoreGrid)
{
// Schreibe deinen Code hier
}
}
// 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)
{
// Überprüfe auf out-of-Bounds-Indizes
if (studentIndex < 0 || studentIndex >= scoreGrid.Length ||
assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
{
return -1;
}
// Validiere die Note
if (!DataCollector.ValidateScore(score))
{
return -2;
}
// Setze die Note
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)
{
// Überprüfe, ob der Studentenindex gültig ist
if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
{
return -1;
}
int sum = 0;
int count = 0;
// Berechne Summe der gültigen Noten
for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
{
int score = scoreGrid[studentIndex][j];
if (score != -1) // Ignoriere unbewertete Aufgaben
{
sum += score;
count++;
}
}
// Gib Durchschnitt zurück oder 0 wenn keine gültigen Noten
return count > 0 ? (double)sum / count : 0;
}
public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
{
// Überprüfe, ob Studenten vorhanden sind
if (scoreGrid.Length == 0)
{
return -1;
}
// Überprüfe, ob der Aufgabenindex gültig ist
if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
{
return -1;
}
int sum = 0;
int count = 0;
// Berechne Summe der gültigen Noten für die Aufgabe
for (int i = 0; i < scoreGrid.Length; i++)
{
if (assignmentIndex < scoreGrid[i].Length)
{
int score = scoreGrid[i][assignmentIndex];
if (score != -1) // Ignoriere unbewertete Aufgaben
{
sum += score;
count++;
}
}
}
// Gib Durchschnitt zurück oder 0 wenn keine gültigen Noten
return count > 0 ? (double)sum / count : 0;
}
public static int[] FindHighestScore(int[][] scoreGrid)
{
int highestStudentIndex = 0;
int highestAssignmentIndex = 0;
int highestScore = -1;
// Suche nach der höchsten Note
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 };
}
}Alle Lektionen in Logik & Ablauf
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