MilaDimitrovaa

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

Feb 28th, 2021
1,105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×