Dua Skor Tinggi

Assalamualaikum. .
Setelah kemaren saya membagikan tentang skor tinggi, kali ini saya akan membagikan tentang skor tinggi lanjutan pada pemrograman java, ..
Selamat mencoba,..

import java.util.Scanner;

public class TwoHighestScores {

public static void main(String[] iriahus) {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);

   //ask user for number of students
   System.out.print("Enter the number of students: ");
   int numberStudents = s.nextInt();

   //declare variables
   double highestScore = 0;
   double secondHighestScore = 0;
   String firstStudent = "";
   String secondStudent = "";

   for (int i = 0; numberStudents != i; i++) {
       System.out.print("Enter the students name followed by his score: ");
       String studentName = s.next();
       double studentScore = s.nextDouble();

       if (i < 0) {
           System.out.println("Wrong input please restart..");
           break;
       }
     
       if (i == 0) {
           firstStudent = studentName;
           highestScore = studentScore;
       }

       else if (studentScore > highestScore) {
        secondStudent = firstStudent;
        secondHighestScore = highestScore;
        firstStudent = studentName;
        highestScore = studentScore;
       }
   }  
   System.out.println(firstStudent + " is the highest score of " + highestScore);
   System.out.println(secondStudent + " is the second highest score of " + secondHighestScore);
}

}

Semoga bermanfaat, . .

Subscribe to receive free email updates:

0 Response to "Dua Skor Tinggi"

Post a Comment