Enter·the·number·of·students:6 Student·1·of·6 Enter·student\'s·name:Tucker·Mansf
ID: 3687014 • Letter: E
Question
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·FitzPatrick Enter·student's·score:78.80 Student·4·of·6 Enter·student's·name:Amy·Dickstein 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·highest·score·was·96.70·and·Thomas·Collins·got·it The·second·highest·score·was·93.30·and·Amy·Dickstein·got·it in java please.
Explanation / Answer
import java.util.Scanner;
public class Student {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = sc.nextInt();
String firstStudent = "";
String secondStudent = "";
double firstMax = 0;
double secondMax = 0;
String name;
double mark;
for(int i=1; i<=n; i++){
System.out.println("Student "+i+" of "+n);
sc.nextLine(); // Consume newline left-over
System.out.print("Enter student's name: ");
name = sc.nextLine();
System.out.print("Enter student's score: ");
mark = sc.nextDouble();
if(i==1){
firstMax = mark;
secondMax = mark;
firstStudent = name;
secondStudent = name;
}
if(mark > firstMax){
secondMax = firstMax;
secondStudent = firstStudent;
firstMax = mark;
firstStudent = name;
}else if(mark > secondMax){
secondMax= mark;
secondStudent = name;
}
}
System.out.println("The higest score was "+String.format("%.2f", firstMax)+" and "+firstStudent);
System.out.println("The second higest score was "+String.format("%.2f", secondMax)+" and "+secondStudent);
}
}
/*
Sample run:
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 Fitz
Enter student's score: 78.80
Student 4 of 6
Enter student's name: Amy Dickstein
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 higest score was 96.70 and Thomas Collins
The second higest score was 93.30 and Amy Dickstein
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.