Advertisement
coasterka

Task07CorrectButWithWrongCondition

Nov 15th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. public class Task07CorrectButWithWrongCondition {
  2.    
  3.     public static void main(String[] args) {
  4.         int[][] arr = { { 11, 12, 13, 14, 15, 16 },
  5.                 { 21, 22, 23, 24, 25, 26 },
  6.                 { 31, 32, 33, 34, 35, 36 },
  7.                 { 41, 42, 43, 44, 45, 46 },
  8.                 { 51, 52, 53, 54, 55, 56 },
  9.                 { 61, 62, 63, 64, 65, 66 } };
  10.         // sum of the elements whose indexes are as follows:
  11.         // if even row -> even index
  12.         // if odd row -> odd index
  13.         int row = 0;
  14.         int col = 0;
  15.         int tempSum = 0;
  16.         int rowSum = 0;
  17.         int sumOfElements = 0;
  18.         while (row < arr.length && col < arr[0].length) {
  19.             tempSum = 0;
  20.             if (row % 2 == 0) {
  21.                 if (col % 2 == 0) {
  22.                     tempSum += arr[row][col];
  23.                     rowSum += tempSum;
  24.                     System.out.print(arr[row][col] + ", ");
  25.                     if (col == arr[0].length - 1) {
  26.                         row++;
  27.                         col = 0;
  28.                     }
  29.                     else {
  30.                         col++;
  31.                     }
  32.                 }
  33.                 else {
  34.                     if (col == arr[0].length - 1) {
  35.                         System.out.println("Сума от елементите за реда: " + rowSum);
  36.                         row++;
  37.                         col = 0;
  38.                         rowSum = 0;
  39.                     }
  40.                     else {
  41.                         col++;
  42.                     }
  43.                 }
  44.             }
  45.             else if (row % 2 == 1) {
  46.                 if (col % 2 == 1) {
  47.                     tempSum += arr[row][col];
  48.                     rowSum += tempSum;
  49.                     System.out.print(arr[row][col] + ", ");
  50.                     if (col == arr[0].length - 1) {
  51.                         System.out.println("Сума от елементите за реда: " + rowSum);
  52.                         row++;
  53.                         col = 0;
  54.                         rowSum = 0;
  55.                     }
  56.                     else {
  57.                         col++;
  58.                     }
  59.                 }
  60.                 else {
  61.                     if (col == arr[0].length - 1) {                    
  62.                         row++;
  63.                         col = 0;
  64.                     }
  65.                     else {
  66.                         col++;
  67.                     }
  68.                 }
  69.             }
  70.             sumOfElements += tempSum;
  71.         }
  72.         System.out.println("Сума на елементите: " + sumOfElements);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement