java - how to find two's best answers -
I'm trying to find two largest numbers from the console. I got it for the first time, but the solution for the other is not working. The program is compiling and running. Here's the code.
Import java.util.Scanner; Finding public squaresCancersChemicalScgi09 {public static zero main (string [] args) {scanner input = new scanner (system.); Double max = 1; Double score 2 = 0; String firstname = ""; String secondName = null; System.out.println ("Enter the number of students:"); Int x = input.nextInt (); While (x> 0) {System.out.println ("Enter the name of the suente"); String name = input.next (); System.out.println ("Enter student's score"); Double score = input NnxDouble (); // get maximum (score> max) {max = score; First name = name; } // Find the second maximum (maximum lie 2) {max = score2; Score = Score 2; } And if (Max> Score 2 & amp; Score 2 & lt; score) {Score 2 = Score; SecondName = Name; } X--; } System.out.println ("The student:" + firstName + "has the largest score:" + max); System.out.println ("second stop" + second name + "with second result:" + score 2); Here's a slightly more detailed implementation (waking up today's awareness):
import java.util.ArrayList; Import java.util.list; Import java.util.Scanner; Public square topscore {Private constant final integer TOP_SELECTION_SIZE = 2; Public Stable Class Student {Private Last String Name; Private double score; Public student (name of string) {if (name == faucet} name.length () == 0} setting new invalid records ("name can not be empty"); } This.name = name; } Public string getName () {return name; } Public double getScore () {Return score; } Public Zero Set Score (String Score) {try.score = double.parseDouble (Score)); } Hold (NumberFormatException e) {new invalid execution exception ("invalid score:" + score); }} @ Override public string toring () {string.form return ("with% s score% s", name, score); }} Public static zero main (string [] args) {list & lt; Students & gt; Students = New Arrestist & Lieutenant TopScores.Student & gt; (); System.out.println ("Please enter students, press & lt; back & gt; to stop."); Scanner input = new scanner (System.in); Boolean Insert = true =; While {signing} {try {System.out.print ("Enter the student's name:"); Student student = new student (input.NXTine); System.out.print ("Enter the student's score:"); Student.setScore (input.nextLine ()); For (int i = 0; i & lt; students.size (); i ++) {if (student.getScore () & gt; students.gate (i) .getScore ()) {students.add (i , student); break; }} If (students.size () == 0) {students.add (student); }} Hold (invalid record exception e) {enterData = false; }} Int'l studentship = math. (Top_ SELECTION_SIZE, students. size ()); If (studentsodaysplay gt; 0) {System.out.println ("Top students:"); {System.out.println ("*" + students.get (i)) for (Int i = 0; I & lt; studentsToDisplay; i ++); }} And {System.out.println ("No students to display"); }}} I have created a separate class that puts student names and numbers, validates input and creates a display format for a student.
To determine the top digits, I add all the new students to the correct position and sort all enrolled students into the list.
Users can end data entry by entering an empty line (or an invalid score) already to students.
After the data entry is over, the desired number of students with top scoring has been printed.
This approach is more flexible; Printing top 3 or top 10 students is a matter of changing the value of TOP_SELECTION_SIZE.
The most important deficiencies: Try to think in classrooms (students in this case) where possible and appoint sensible responsibilities for each class.
Comments
Post a Comment