Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Pertemuan1;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class YesYes2 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- ArrayList<Double> nilaiKelasA = new ArrayList<>();
- ArrayList<Double> nilaiKelasB = new ArrayList<>();
- while (true) {
- System.out.println("\nMenu:");
- System.out.println("1. Input Nilai Kelas A");
- System.out.println("2. Input Nilai Kelas B");
- System.out.println("3. Lihat Statistik Kelas A");
- System.out.println("4. Lihat Statistik Kelas B");
- System.out.println("5. Keluar");
- System.out.print("Pilih menu (1/2/3/4/5): ");
- int pilihan = input.nextInt();
- switch (pilihan) {
- case 1:
- System.out.print("Masukkan nilai PBO untuk Kelas A: ");
- double nilaiA = input.nextDouble();
- nilaiKelasA.add(nilaiA);
- break;
- case 2:
- System.out.print("Masukkan nilai PBO untuk Kelas B: ");
- double nilaiB = input.nextDouble();
- nilaiKelasB.add(nilaiB);
- break;
- case 3:
- tampilkanStatistik("Kelas A", nilaiKelasA);
- break;
- case 4:
- tampilkanStatistik("Kelas B", nilaiKelasB);
- break;
- case 5:
- System.out.println("Terima kasih! Program selesai.");
- System.exit(0);
- default:
- System.out.println("Pilihan tidak valid. Silakan coba lagi.");
- }
- }
- }
- private static void tampilkanStatistik(String kelas, ArrayList<Double> nilaiKelas) {
- if (nilaiKelas.isEmpty()) {
- System.out.println("Tidak ada nilai PBO yang tersimpan untuk " + kelas);
- return;
- }
- double rataRata = hitungRataRata(nilaiKelas);
- double nilaiTertinggi = Collections.max(nilaiKelas);
- double nilaiTerendah = Collections.min(nilaiKelas);
- System.out.println("Statistik untuk " + kelas + ":");
- System.out.println("Nilai Tersimpan: " + nilaiKelas);
- System.out.println("Rata-rata: " + rataRata);
- System.out.println("Nilai Tertinggi: " + nilaiTertinggi);
- System.out.println("Nilai Terendah: " + nilaiTerendah);
- }
- private static double hitungRataRata(ArrayList<Double> nilaiKelas) {
- double total = 0;
- for (double nilai : nilaiKelas) {
- total += nilai;
- }
- return total / nilaiKelas.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement