Advertisement
sergAccount

Untitled

Mar 7th, 2021
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication4;
  7.  
  8. /**
  9.  *
  10.  * @author Admin
  11.  */
  12. public class JavaApplication4 {
  13.    
  14.     /*
  15.     Задача1
  16.     Создать двумерный массив значений типа double
  17.     Заполнить его числами  
  18.     Создать метод который находит сумму элементов
  19.     двумерного массива    
  20.     */    
  21.     public static double calcSum(double[][] arr){
  22.         double res = 0;
  23.         for (int i = 0; i < arr.length; i++) {
  24.             for (int j = 0; j < arr[i].length; j++) {
  25.                 res += arr[i][j];
  26.             }
  27.         }
  28.         return res;
  29.     }    
  30.     /*
  31.     Задача2
  32.     Создать двумерный массив значений типа int
  33.     Заполнить его числами  
  34.     Создать метод который находит сумму элементов
  35.     которые находятся на главной диагонали
  36.     */
  37.     public static int calcDiagSum(int[][] arr){
  38.         int res = 0;
  39.         for (int i = 0; i < arr.length; i++) {
  40.             int arrLen = arr[i].length;
  41.             if(i<arrLen){
  42.                 res += arr[i][i];
  43.             }
  44.             //if(i==j){
  45.             //}
  46.         }
  47.         return res;
  48.     }    
  49.     /**
  50.      * @param args the command line arguments
  51.      */
  52.     public static void main(String[] args) {
  53.         // TODO code application logic here
  54.         double[][] arr = {
  55.             {1.0, 2.0},
  56.             {1.0, 2.0}
  57.         };
  58.         double result = calcSum(arr);
  59.         System.out.println("result=" + result);
  60.        
  61.         int[][] arr2 = {
  62.             {0, 2},
  63.             {3, 8},
  64.             {9, 10}                        
  65.         };
  66.         // 1 - [0][0] 4 - [1][1]
  67.         int res = calcDiagSum(arr2);
  68.         System.out.println("res=" + res);
  69.     }
  70.    
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement