I am trying to create a program that takes user input for the amount of students and then with that in mind prompt for 5 grades and an average per student.
An example of a student record would be as follows:
Student #...... Grade1..... Grade2..... Grade3..... Grade4..... Grade5..... Average
Code so far (Probably not correct in anyway):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question_Two
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
Student[] students = new Student[6];
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId)); // or error here
double value;
value = double.Parse(Console.ReadLine());
students[1].addGrade(value);
value = double.Parse(Console.ReadLine());
students[2].addGrade(value);
value = double.Parse(Console.ReadLine());
students[3].addGrade(value);
value = double.Parse(Console.ReadLine());
students[4].addGrade(value);
value = double.Parse(Console.ReadLine());
students[5].addGrade(value);
students[6].getAverage();
}
public class Student
{
private ArrayList grades;
public Student()
{
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
}
I am not sure where to go from here as I have an error when storing the name into the array.
You can not parse a string "Student 1" to double like in the following code:
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId));
I don't know what you want to the result to be. But make sure that the student id is a numeric value (can be in string form). You can use the following code to get what you want.
Console.WriteLine("Input the number of students: ");
int numberOfStudents = int.Parse(Console.ReadLine());
Student student = new Student();
string studentId = "1";
student.addGrade(double.Parse(studentId));
double value;
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
double avarageNumber = student.getAverage();
Creating an array with new
does not create each individual element (except primitives like int
). You need to call new
for every item in the array:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
// Create array of size n (instead of 6)
// Array is created but individual Students are not created yet!
Student[] students = new Student[n];
// Use a loop to get all student info
for (int s = 0; s < n; s++) {
string studentId = $"Student{s+1}";
// Important! Need to create a student object
students[s] = new Student(studentId);
// Use another loop to get grades
for (int g = 0; g < 5; g++) {
double grade = double.Parse(Console.ReadLine());
students[s].addGrade(grade);
}
// Print average
Console.WriteLine($"{students[s].id} average = {students[s].getAverage()}");
}
}
Also, modify the Student
class to have an id
public class Student
{
private ArrayList grades;
public string id;
// Accepts one parameter : an id
public Student(string studentId)
{
id = studentId;
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.