DRAFT: This module has unpublished changes.

Here you will find a specific assignment given to us in class. As instructed:

Write a program that prompts the user to enter the number of students and each students name and score, and finally displays the name of the student with the highest score.

 

To explain, I began by creating what is called a "Scanner" that saves input from the keyboard (such as a name or a number) and saves this to a variable to be used later. This variable is actually the number of students that are to be scored.

 

After, I create two more variables. Another number that has a 2-digit decimal (such as 100.00) for the Highscore of the Student as well as a String variable, which takes non-numeric input (such as a name) to serve as the students name.

 

Finally I create conditions for a loop. A loop is what you might think it is, a repeating code. Though this code only repeats as long as its conditions are met. Such as at the end of each loop or one iteration of the loop, a variable counter will increase by 1. Once this variable is equal to the number of students (recorded by the scanner), the loop stops.

 

Within this loop, every time it runs it takes new input from the user. The user inputs the student's name followed by their score.

 

Earlier we made a floating variable (2-digit decimal) for the Highscore. This was initially set to be 0.00. Once the user enters the students score, it will replace the old Highscore value with the new score and know that the name given afterwards belongs to that score.

 

The loop will repeat itself until all students and scores are entered. Each time it completes one loop it will compare the NEW score with the HIGHSCORE and if the new score is lower, it is ignored (as is the student name). If the NEW score is HIGHER then it will become the new HIGHSCORE with the new student name replacing the old as well.

 

Once the loop is completed, it will display the resulting variables: TopStudent and Highscore.

 

Here is the code along with the file that contains it. A double backslash indicates a comment was made and does not affect the code.

ClassScore.java

ClassScore.class

 

import java.util.Scanner;

public class ClassScore {
public static void main(String[] students) {

// Create Scanner for string
Scanner input = new Scanner(System.in);

// Find the number of students
System.out.print("How many students are there? ");

int numStudents = input.nextInt();

// Set initial value for highest score

float highscore = 0;
String topStudent = new String();

// Set the number of loops to match the number of students

for(int s = 0; s < numStudents; s += 1) {

// Acquire students name
System.out.println("What is the students name? ");
String studentName = input.next();

// Acquire students grade
System.out.println("What is the students score? ");
float score = input.nextInt();

// Associate the students name with their grade

System.out.println("The student " + studentName + " has a grade of " + score + ".");

// Update highest score.
// If the new score is higher than the highscore, it replaces it.
if (score > highscore) {
highscore = score;
topStudent = studentName;
}

}
System.out.println("The student " + topStudent + " had the highest score of " + highscore + ".");
}
}

DRAFT: This module has unpublished changes.