So I am working on a homework right now, but am having trouble trying to figure out a working program for my homework's objective. How it works is I input my class' names, their student IDs(numbers only), and their grade for a subject. The final should be to show the student's name, ID, and their highest grade in the subject.
static void Main(string[] args)
{
int counter = 0;
int number = 0;
int largest = 0;
int studentid = 0;
for (counter = 0; counter < 10; counter )
{
Console.WriteLine("Please enter student name: ");
string studentname = Convert.ToString(Console.ReadLine());
Console.WriteLine("Please enter student ID no.: ");
studentid = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter student's FoP Prelim Grade: ");
number = Convert.ToInt32(Console.ReadLine());
if (counter == 0)
{
largest = number;
}
else
{
if (number > largest)
largest = number;
}
}
Console.WriteLine("{0}, Student ID: {1}, Has the highest grade of {3}", studentname, studentid, number);
}
I apologize if my codes seem messy, and thank you in advance!
CodePudding user response:
If you are just trying to print out the Name, ID, and score for the student with the highest score, you can do that by storing all of the parameters in separate arrays. E.g., names in a string array, IDs and scores in int arrays. All of them will have the same array length, and thus you can iterate through the score array to find the largest score and then print the Name & ID using that index in your Console.WriteLine statement.
CodePudding user response:
If you want to use the easiest way, you can store the values in variables like this:
static void Main(string[] args)
{
int bestGrade = 0;
int bestStudentid = 0;
string bestStudentname = "";
for (var counter = 0; counter < 10; counter )
{
Console.WriteLine("Please enter student name: ");
var studentname = Console.ReadLine();
Console.WriteLine("Please enter student ID no.: ");
var studentid = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter student's FoP Prelim Grade: ");
var grade = Convert.ToInt32(Console.ReadLine());
if (counter == 0)
{
bestGrade = grade;
}
else
{
if (grade > bestGrade)
{
bestGrade = grade;
bestStudentid = studentid;
bestStudentname = studentname;
}
}
}
Console.WriteLine($"{bestStudentname}, Student ID: {bestStudentid}, Has the highest grade of {bestGrade}");
}
If you want to store everything, you have to save them in a list or array and print it with the index of the largest grade.