MilaDimitrovaa

Задача 4 - ООП

Mar 25th, 2021
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scan = new Scanner(System.in);
  10.  
  11.         System.out.println("Please input element:");
  12.         int row = scan.nextInt();
  13.         System.out.println("Please input element:");
  14.         int col = scan.nextInt();
  15.  
  16.  
  17.         int numbers[][] = new int[row][col];
  18.  
  19.         for (int r = 0; r < numbers.length; r++) {
  20.             for (int c = 0; c < numbers[r].length; c++) {
  21.                 System.out.println(" [ " + r + " ] [ " + c + " ] : ");
  22.                 numbers[r][c] = scan.nextInt();
  23.             }
  24.         }
  25.         System.out.println();
  26.  
  27.         Print(numbers);
  28.         System.out.println();
  29.  
  30.         for (int rows = 0;rows < numbers.length ; rows++) {
  31.             int [] currentRow = numbers[rows];
  32.             int sum = 0;
  33.             int max = Integer.MIN_VALUE;
  34.             int min = Integer.MAX_VALUE;
  35.             for (int cols = 0; cols < currentRow.length; cols++) {
  36.                 int currentElement = currentRow[cols];
  37.  
  38.                 if(max < currentElement){
  39.                     max = currentElement;
  40.                 }
  41.                 if(min > currentElement){
  42.                     min = currentElement;
  43.                 }
  44.                 sum += currentElement;
  45.             }
  46.  
  47.             double average = (double) sum / (double) currentRow.length;
  48.             String Average = String.format("%.2f" ,average);
  49.  
  50.             System.out.println();
  51.             System.out.printf("Index: %d  Sum: %d  Max: %d  Min: %d  Average: %s" , rows , sum , max , min , Average);
  52.             System.out.println();
  53.         }
  54.     }
  55.  
  56.     public static void Print(int numbers [][]) {
  57.         for (int i = 0; i < numbers.length; i++) {
  58.             for (int j = 0; j < numbers.length ; j++) {
  59.                 System.out.print(numbers[i][j] + " ");
  60.             }
  61.             System.out.println();
  62.         }
  63.      }
  64.     }
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment