Kali ini saya akan tentang Perangkingan, ada udah pernah dengar tentan perangkingan, .?
Klo belum silahkan di coba langsung biar tidak pusing-pusing, .. hhe
import java.util.Scanner;
public class Perangkingan {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("Masukkan banyaknya data\t: ");
int n = input.nextInt();
String[] nama = new String[n];
String[] npm = new String[n];
int[] nilai = new int[n];
System.out.print("Masukkan " + n + " nama\t\t: ");
for (int i = 0; i < n; i++) {
nama[i] = input.next();
}
System.out.print("Masukkan " + n + " npm\t\t: ");
for (int i = 0; i < n; i++) {
npm[i] = input.next();
}
System.out.print("Masukkan " + n + " nilai\t\t: ");
for (int i = 0; i < n; i++) {
nilai[i] = input.nextInt();
}
tampilData(n, nama, npm, nilai);
rangkingData(n, nama, npm, nilai);
}
private static void tampilData(int n, String[] nama, String[] npm, int[] nilai) {
System.out.println("\n" + n + " buah data sebelum dirangking:\n");
System.out.println("-----------------------------------------");
System.out.println("No.\tNama\tNPM\tNilai\tRangking");
System.out.println("-----------------------------------------");
for (int i = 0; i < n; i++) {
System.out.println(i + 1 + ".\t" + nama[i] + "\t" + npm[i] + "\t" + nilai[i]);;
}
}
private static void rangkingData(int n, String[] nama, String[] npm, int[] nilai) {
for (int i = 0; i < nilai.length; i++) {
for(int j = 1; j < nilai.length; j++) {
if (nilai[j] > nilai[j-1]) {
int temp1 = nilai[j];
String temp2 = nama[j];
String temp3 = npm[j];
nilai[j] = nilai[j-1];
nama[j] = nama[j-1];
npm[j] = npm[j-1];
nilai[j-1] = temp1;
nama[j-1] = temp2;
npm[j-1] = temp3;
}
}
}
System.out.println("\n" + n + " buah data setelah dirangking:\n");
System.out.println("-----------------------------------------");
System.out.println("No.\tNama\tNPM\tNilai\tRangking");
System.out.println("-----------------------------------------");
int x = 0;
for (int i = 0; i < n; i++) {
System.out.println(i + 1 + ".\t" + nama[i] + "\t" + npm[i] + "\t" + nilai[i] + "\t" + (x+1));
x++;
if (nilai[i+1] == nilai[i])
x--;
}
}
}
Semoga bermanfaat, .
0 Response to "Perangkingan"
Post a Comment