0
public class Exercise_09 {

   public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of student: ");
    int studentCount = input.nextInt();
    input.nextLine();

    String topSName = null;
    double topSScore = 0;
    String secondSName = null;
    double secondSScore = 0;

    for (int i = 0; i < studentCount; i++) {
        System.out.print("Enter name for student #" + (i + 1) + ": ");
        String s = input.next();

        System.out.print("Enter score for student #" + (i + 1) + ": ");
        double score = input.nextDouble();

        if (score > topSScore) {
            if (topSName != null) {
                secondSName = topSName;
                secondSScore = topSScore;
            }
            topSName = s;
            topSScore = score;
        } else if (score > secondSScore) {
            secondSName = s;
            secondSScore = score;
        }

    }
    System.out.println("Top student " + topSName + "'s score is " + topSScore);
    System.out.println("Second top student " + secondSName + "'s score is " + secondSScore);
}

}

Ok I got this code what it does is it ask for number of students, their names and score and displays the highest and 2nd highest score.

Now my question is that when I am repeating asking for name of the student and score how input keep the record of all the scores and names and how do i call it ? WHat the hell that if statement logic doing? i dont get that part.

k31453
  • 17
  • 5

3 Answers3

1

You can use arraylist to store name and score.

After storing data into arraylist you can display data from arraylist.

Here http://www.careerbless.com/samplecodes/java/beginners/collections_ds/inertandretrieveArrayList.php you can get example how to insert and retrieve value from arraylist.

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • Ok but what is this logic code using? the if statement in for loop. it is not getting in my head – k31453 Dec 26 '15 at 06:50
0

You would want to use an object that implements Map<T>, an interface in java that supports key with value, or you can simply use the interface Map<T> itself. You have a student and a score associated with the student, therefore you need a key and value.

https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

Store a key and value:

Map.put(key, value);

Get a value from a key:

Map.get(key);

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
0

Here's what your if statements are doing in for loop.

  1. But before First Initialize variable : topScore = 0; secondSScore= 0;(lowest).(My suggestion: Initialized it to -1).

  2. Inside Loop: (Now here your logic is divided into two main parts)

    1. FIRST ITERATION

      1. Get the Students score.
      2. if score is greater than topScore then assign topScore the value of score.

(Now here in First iteration the score of first student is set as the topScore and also the second topScore (secondSScore)).

Why ?... Because the value of topScore is initialized to 0 which is less than first student's score(only if first student has not scored zero). This tells us until now according to received data this is the top Score as well as the second topScore.(Pretty logical ..)

 // First iteration 
 if (score > topSScore) {  // topSScore = 0 
            topSName = s;
            topSScore = score;
        } else if (score > secondSScore) { //secondSScore = 0
            secondSName = s;
            secondSScore = score;
        }
  1. REST OF ITERATION

Now in further iteration if any student's score is greater than new topScore then

FIRST assign secondSScore the value of topSScore

      if (topSName != null) {
            secondSName = topSName;
            secondSScore = topSScore;
      }

and then assign the topSSCore the value of student's score and so on....phew

Hope I explained well.

Its like find largest and second largest from given data (commonly an array).

Here's some similar example:

  1. Finding-the-second-highest-number-in-array
  2. Find two-max-numbers-in-array
Community
  • 1
  • 1
rhitz
  • 1,892
  • 2
  • 21
  • 26