MilaDimitrovaa

Задача за диагонали

Feb 28th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 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.         Scanner scan = new Scanner(System.in);
  9.         int rows = scan.nextInt();
  10.         int cols = scan.nextInt();
  11.  
  12.  
  13.         int numbers[][] = new int[rows][cols];
  14.  
  15.  
  16.         for (int r = 0; r < numbers.length; r++) {
  17.             for (int c = 0; c < numbers[r].length; c++) {
  18.  
  19.  
  20.                 System.out.println(" [ " + r + " ] [ " + c + " ] : ");
  21.                 numbers[r][c] = scan.nextInt();
  22.             }
  23.         }
  24.         Print(numbers);
  25.         Diagonal(numbers);
  26.     }
  27.  
  28.     public static void Print(int numbers[][]) {
  29.         for (int i = 0; i < numbers.length; i++) {
  30.             for (int j = 0; j < numbers.length; j++) {
  31.                 System.out.print(numbers[i][j] + " ");
  32.  
  33.             }
  34.             System.out.println();
  35.         }
  36.     }
  37.     public static void Diagonal(int numbers [][]){
  38.  
  39.         int d1 = 0;
  40.         int d2 = 0;
  41.         for (int i = 0; i < numbers.length ; i++) {
  42.             d1 += numbers[i][i];
  43.             d2 += numbers[i][numbers.length - 1 - i];
  44.         }
  45.         System.out.println("Print diagonal 1 : " + d1);
  46.         System.out.println("Print diagonal 2 : " + d2);
  47.             }
  48.  
  49.     }
  50.  
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment