HayCZ

izapr-cv04

Mar 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package izapr_cv04;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Izapr_cv04 {
  6.     public static void main(String[] args) {
  7.           int[] pole = new int[50];
  8.           int min = 1, max = 90;
  9.           naplnPole(pole, min, max);
  10.           vypisPole(pole);
  11.           System.out.println("Součet prvků pole je:" + soucetPrvku(pole));
  12.           nejvetsiCislo(pole, min, max);
  13.           System.out.println("" + Arrays.toString(pole)); // Jiná možnost zápisu výpisu.
  14.     }
  15.     public static void nejvetsiCislo(int pole[], int max, int min)
  16.     {
  17.         int indexMin = -1, indexMax = -1;
  18.         for (int i = 0; i < pole.length; i++) {
  19.             if (max <= pole[i]) {
  20.                 max = pole[i];
  21.                 indexMax = i;
  22.             }
  23.             if (min >= pole[i]) {
  24.                 min = pole[i];
  25.                 indexMin = i;
  26.             }
  27.         }
  28.         System.out.println("Největší číslo v poli je:" + max + " na pozici: " + indexMax);
  29.         System.out.println("Nejmenší číslo v poli je:" + min + " na pozici: " + indexMin);
  30.     }
  31.     public static void naplnPole(int pole[], int min, int max){
  32.         for (int i = 0; i < pole.length; i++) {
  33.             pole[i] = generujCislo(min,max);
  34.         }
  35.     }
  36.     public static int soucetPrvku(int pole[])
  37.     {
  38.         int soucet = 0;
  39.         for (int i = 0; i < pole.length; i++) {
  40.             soucet += pole[i];
  41.         }
  42.         return soucet;
  43.     }
  44.     public static void vypisPole(int pole[]){
  45.         for (int i = 0; i < pole.length; i++) {
  46.             //for(prvek:pole)
  47.             System.out.print(pole[i] /*nebo prvek*/+",");
  48.         }
  49.          System.out.println();
  50.     }
  51.     public static int generujCislo(int min, int max){
  52.         return (int) Math.round(Math.random() * (max - min) + min);
  53.     }
  54.    
  55. }
Advertisement
Add Comment
Please, Sign In to add comment