Advertisement
MilaDimitrovaa

Задача 6

May 13th, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void print(int[][] matrix){
  7.         Scanner scan = new Scanner(System.in);
  8.         for (int row = 0; row < matrix.length; row++) {
  9.             for (int col = 0; col < matrix.length; col++) {
  10.                 System.out.printf("[%d] row [%d] col: ",row ,col);
  11.                 matrix[row][col] = scan.nextInt();
  12.             }
  13.         }
  14.     }
  15.     public static void main(String[] args) {
  16.  
  17.         Scanner scanner = new Scanner(System.in);
  18.         System.out.print("Enter row and cols :");
  19.  
  20.         int rowAndColLength = scanner.nextInt();
  21.         int[][] arr = new int[rowAndColLength][rowAndColLength];
  22.  
  23.         print(arr);
  24.  
  25.         System.out.println();
  26.         for (int row = 0; row < arr.length; row++) {
  27.             for (int col = 0; col < arr.length; col++) {
  28.                 System.out.print(arr[row][col] + " ");
  29.             }
  30.             System.out.println();
  31.         }
  32.         System.out.println();
  33.  
  34.         for (int row = 0; row < arr.length; row++) {
  35.  
  36.             for (int col = 0; col < arr.length; col++) {
  37.                 int sum = 0;
  38.  
  39.                 if(col + 1 < arr.length){
  40.                     sum = sum + arr[row][col] + arr[row][col+1];
  41.                     System.out.println();
  42.                     System.out.printf("Sum --> row[%d]: col[%d] + col[%d] is: %d",row,col,col+1,sum);
  43.                     System.out.println();
  44.                 }
  45.  
  46.             }
  47.  
  48.         }
  49.     }
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement