I have 2 tables, the first one displays the name of the student, and the second one displays the marks for the student. each student has 2 semesters. I wrote it like this:
import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
Scanner Sc=new Scanner(System.in);
int Number,i,j,q,n;
System.out.println(" how many student in your group ?");
Number=Sc.nextInt();
//create the Array
String [] tab =new String [Number];
for(n=0;n<Number;n++)
{
System.out.println(" Enter studnt name : "+(n + 1) );
tab[n]=Sc.nextLine();
}
int [][] marks=new int [2][Number];
for(j=0;j<Number;j++)
{
for (i=0;i<1;i++)
{
System.out.println(" Enter the marks of the first semster : "+(j + 1));
marks [i][j]=Sc.nextInt();
}
for (q=1;q<2;q++)
{
System.out.println(" Enter the marks of the Second semseter : "+(j + 1));
marks [q][j]=Sc.nextInt();
}
}
}
}
i
= for the first rows which will display the marks for the first semester for each student
q
= for the second rows which will display the marks for the second semester for each student
Now I want to find the total marks for each student and the highest marks. ( highest marks for one student mean he/she gets the highest marks in which semester and the total also for the one student total = first semester + second semester). But I don't know how I will write it if anyone can help me I would be grateful to him.
Before calculating highest mark and total marks for each student, you need two create two arrays to store that data. The size of the array will be the number of students.
int[] totalMarks = new int[studentsCount];
int[] highestMark = new int[studentsCount];
Once you have these, total and highest marks can be calculated in two different loops or one single loop.
For any student i
,
highestMark[i] = max(marks[0][i], marks[1][i])
totalMarks[i] = marks[0][i] + marks[1][i]
To do the same process for all the students, use a simple for loop from i = 0
to studentCount
.
Your program can also be improved. Start giving meaningful name to your variables. Instead of NUMBER
use studentCount
. Even though Sc
works okay, scanner
is better. Another improvement is:
for(j=0;j<studentCount;j++)
{
System.out.println ("For student " + (j+1) + ": ");
System.out.println("Enter the marks of the first semster: ");
marks [0][j]= scannner.nextInt();
System.out.println("Enter the marks of the second semseter: ");
marks [1][j]= scanner.nextInt();
}
You do not need for loop to input the mark for any student i
for two semester.
I have not provided any code. I have given many hints which should help you solve the problem on your own. However, if you still face any problem. Do comment.