PLEASE USE LOOPS ONLY TO COMPLETE THIS ASSIGNMENT.
(Find the three lowest 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 lowest score and the student with the
second-lowest score and the student with the third lowest
score.
Program must accept first name only or first and last name
SAMPLE RUN #1: java ThreeLowestScores
Enter the number of students:6↵
Student 1 of 6↵
Enter·student's name: Tucker Mansfield↵
Enter student's score: 33.33↵
Student 2 of 6↵
Enter student's name: Barbara Stevenson↵
Enter student's score: 89.56↵
Student 3 of 6↵
Enter student's name: Scott↵
Enter student's score: 78.80↵
Student 4 of 6↵
Enter student's name: Amy Rickstein↵
Enter student's score: 93.3↵
Student 5 of 6↵
Enter student's name:Thomas Collins↵
Enter student's score:96.7↵
Student 6 of 6↵
Enter student's name: Richard Goldstein↵
Enter student's score:85.5↵
The lowest score was 33.33 and Tucker Mansfield got it↵
The second lowest score was 78.80 and Scott got it↵
The third lowest score was 85.5 and Richard Goldstein got it↵
PLEASE USE LOOPS ONLY TO COMPLETE THIS ASSIGNMENTS.
// ThreeLowestScores.java : Java program to input names and scores of n students and display the lowest 3 scores
import java.util.Scanner;
public class ThreeLowestScores {
public static void main(String[] args)
{
int n, j;
String[] names;
double[] scores;
String name;
double score;
Scanner scan = new
Scanner(System.in);
// input the number of
students
System.out.print("Enter the number
of students: ");
n = scan.nextInt();
scan.nextLine(); // ignore \n left
by nextInt
if(n > 0) // validate number of
students > 0
{
// create arrays
to store names and scores of size n
names = new
String[n];
scores = new
double[n];
// loop to input
names and scores of n students and insert it into arrays in sorted
order of score (ascending i.e lowest to highest)
for(int
i=0;i<n;i++)
{
// input the student name and score
System.out.println("Student "+(i+1)+" of
"+n);
System.out.print("Enter student's name:
");
name = scan.nextLine();
System.out.print("Enter student's score:
");
score = scan.nextDouble();
scan.nextLine();
j = i-1;
// loop to insert the read name and score in
array in sorted order by score (i.e lowest to highest)
while(j >= 0 && scores[j] >
score)
{
scores[j+1] =
scores[j];
names[j+1] = names[j];
j--;
}
scores[j+1] = score;
names[j+1] = name;
}
// loop to
display the lowest 3 scores
for(int
i=0;i<n;i++)
{
if(i == 0)
System.out.println("The
lowest score was "+scores[0]+" and "+names[0]+" got it");
else if(i == 1)
System.out.println("The
second lowest score was "+scores[1]+" and "+names[1]+" got
it");
else if(i == 2)
System.out.println("The third
lowest score was "+scores[2]+" and "+names[2]+" got it");
else
break;
}
}else
System.out.println("Number of students must be greater than
0");
}
}
// end of ThreeLowestScores.java
Output:
Get Answers For Free
Most questions answered within 1 hours.