Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package array2Dimensi;
- import java.util.Scanner;
- /**
- *
- * @author aldid
- */
- public class aldidwik_PengambilanData {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in); // instansiasi Scanner
- // deklarasi array nama dan nilai
- String[][] nama;
- int[][] nilai;
- // inputan untuk menentukan panjang array
- System.out.print("Masukkan jumlah baris : ");
- int baris = sc.nextInt();
- System.out.print("Masukkan jumlah kolom : ");
- int kolom = sc.nextInt();
- // inisialisasi panjang array
- nama = new String[baris][kolom];
- nilai = new int[baris][kolom];
- // proses memasukkan elemen array
- for (int i = 0; i < baris; i++) {
- for (int j = 0; j < kolom; j++) {
- System.out.print("Masukkan Nama baris ke-" + i + " kolom ke-" + j + ": ");
- sc.nextLine();
- nama[i][j] = sc.nextLine();
- System.out.print("Masukkan Nilai baris ke-" + i + " kolom ke-" + j + ": ");
- nilai[i][j] = sc.nextInt();
- }
- }
- // menampilkan isi array nama
- System.out.println("\nData nama:");
- for (String[] name : nama) {
- for (String n : name) {
- System.out.print(n + "\t");
- }
- System.out.println();
- }
- // menampilkan isi array nilai
- System.out.println("\nData nilai:");
- for (int[] score : nilai) {
- for (int n : score) {
- System.out.print(n + "\t");
- }
- System.out.println();
- }
- System.out.println();
- // cari modus nilai pada barisan terdepan
- int maxValue = 0, maxCount = 0, duduk;
- for (int i = 0; i < kolom; ++i) {
- int count = 0;
- for (int j = 0; j < kolom; ++j) {
- if (nilai[0][j] == nilai[0][i]) {
- ++count;
- }
- }
- if (count > maxCount) {
- maxCount = count;
- maxValue = nilai[0][i];
- }
- }
- System.out.println("Nilai yang sering muncul pada barisan paling depan: " + maxValue);
- System.out.println("Nama yang mendapatkan nilai tersebut ialah:");
- for (int i = 0; i < kolom; i++) {
- if (nilai[0][i] == maxValue) {
- System.out.println(nama[0][i]);
- }
- }
- System.out.println();
- // cari nilai terbesar dan terkecil tiap baris
- for (int i = 0; i < baris; i++) {
- int max = nilai[i][0];
- int min = nilai[i][0];
- String nameMax = "";
- String nameMin = "";
- for (int j = 0; j < kolom; j++) {
- if (max < nilai[i][j]) {
- max = nilai[i][j];
- nameMax = nama[i][j];
- }
- if (min > nilai[i][j]) {
- min = nilai[i][j];
- nameMin = nama[i][j];
- }
- }
- System.out.println("Nilai terbesar baris ke-" + i + ":");
- System.out.println("\t Nama : " + nameMax);
- System.out.println("\t Nilai : " + max);
- System.out.println("Nilai terkecil baris ke-" + i + ":");
- System.out.println("\t Nama : " + nameMin);
- System.out.println("\t Nilai : " + min);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement