James Liang - ICS Blog

ICS4U Blogging

Arrays - Multiplayer Coding

Multiplayer Coding

Create a program that stores a set of student marks and outputs the highest mark. The program will first ask for the number of students. Based on the number of students, the program will ask for each student’s name and their mark. After all names and marks have been entered, the program will display the student with the highest mark. Use arrays to store your data.
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int numberStudents;

    System.out.print("How many students? ");
    numberStudents = input.nextInt();
    String [][] mark = new String [numberStudents][2];
    for (int row = 0; row < numberStudents; row++)
        System.out.print("Enter Name " + (row + 1) + ": ");
        mark[row][0] = input.nextLine();
        System.out.print("Enter Mark " + (row + 1) + ": ");
        mark[row][1] = input.nextLine();

    int highestMark = Integer.parseInt(mark[0][1]);
    int highestPerson = 0;
    for (int temp = 0; temp < numberStudents; temp++)
      if ((Integer.parseInt(mark[temp][1])) > highestMark)
        highestMark = Integer.parseInt(mark[temp][1]);
        highestPerson = temp; 
    System.out.println("The student with the highest mark is " + mark[highestPerson][0] + " with a mark of " + mark[highestPerson][1] + "%");

Post on your Learning Blog with a DETAILED EXPLANATION of your code, try to use all the programming TERMINOLOGY from this topic

Since I need to ask the user for the number of students, I imported Scanner. I then created a new scanner to read user input. After that I initialized one of my variables. The following lines retrieve the name and marks of the students. First I ask the user how many students there are and then I scan for their input. I use input.nextLine() to fix the issue with scanning Strings after scanning an integer. I then create a 2D array that has numberStudents rows and 2 columns. Since I only need to store the name and mark, I didn’t need any more columns. After the array is created, I used a for loop that would keep looping until it went through all of the students. Inside the loop, I ask and read the input of both the name and mark of the student. I set name to the first column and mark to the second. After reading all of the inputs, it was time to determine the student with the highest mark. I created a highestMark variable and set it to the mark of the first student. I then created a highestPerson variable and set it to 0 (the first student). This way, if there was only one student, they would be the student with the highest mark. I then use a for loop that goes through all of the students. Inside the loop is an if statement, it checks if the mark of the current chosen student is higher than the highestMark variable. If the statement is true, the highestMark variable will be replaced with the chosen student and the highestPerson will be set to the index for that mark. Finally, at the end I outputted the results using the highestPerson variable and math operators.