Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5-9) (Find the two highest scores) Write a program that prompts the user to enter the number of students and each student's name and score,

5-9) (Find the two highest scores) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score and the student with the second-highest score.

In Java please!

Here is what I have so far and MyProgrammingLab is not liking it....help!?!?!

import java.util.*;

public class Main {

public static void main(String[] args) {

// Create a Scanner

Scanner input = new Scanner(System.in);

// Prompt the user to enter the number of students

System.out.print("Enter the number of students: ");

int numberOfStudents = input.nextInt();

int score, // Holds student score

highest = 0, // Highest score

SHighest = 0; // Second highest score

String name = "", // Holds student name

student1 = "", // Highest scoring student name

student2 = ""; // Second highest scoring student name

// Prompt the user to enter each student name and score

for (int i = 0; i < numberOfStudents; i++) {

System.out.print("Enter a student name:");

name = input.next();

System.out.print("Enter a student score:");

score = input.nextInt();

if (i == 0) {

// Make the first student the highest scoring student so far

highest = score;

student1 = name;

}

else if (i == 1 && score > highest) {

// Second student entered scored

// higher than first student

SHighest = highest;

highest = score;

student2 = student1;

student1 = name;

}

else if (i == 1) {

// Second student entered scored

// lower than first student

SHighest = score;

student2 = name;

}

else if (i > 1 && score > highest && score > SHighest) {

// Last student entered has the highest score

SHighest = highest;

student2 = student1;

highest = score;

student1 = name;

}

else if (i > 1 && score > SHighest) {

// Last student entered has the second highest score

student2 = name;

SHighest = score;

}

}

// Display the student with the hightest score

// and the student with the second-hightest score.

System.out.println("Top two students: "

+student1+"'s Score is "+highest+ " "

+student2+"'s Score is "+SHighest);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Management

Authors: Andrew J. DuBrin

9th Edition

538478233, 2900538478235, 978-0538478236

More Books

Students also viewed these General Management questions