Advertisement
Uimin_Maxim

Задача 2

Jan 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. //Уймин Максим
  2. //IT School, 2 группа
  3. //09.10.2015
  4. //Задача 2. Вычислить сумму элементов строк/столбцов
  5. import java.io.PrintStream;
  6. import java.util.Scanner;
  7. public class Exersise_2 {
  8.     public static PrintStream out = System.out;
  9.     public static Scanner scan = new Scanner(System.in);
  10.     public static int [][] CreateMatrix (int I){
  11.         //Функция создаёт матрицу по заданным с клавиатуры данным, заполняет её случайными числами
  12.         int matrix[][] = new int [I][];
  13.         for (int i = 0; i<I;i++){
  14.             matrix[i] = new int [I];
  15.         }
  16.         for (int i = 0; i<I;i++){
  17.             for (int j = 0; j<I;j++){
  18.                 matrix[i][j]=(-10) + (int)(Math.random()*((10-(-10))+1));
  19.             }
  20.         }
  21.         return matrix;
  22.     }
  23.     public static void PrintMatrix(int[][]matrix,int I){
  24.         //Функция выводит матрицу на экран
  25.         for (int i=0;i<I;i++){
  26.             for (int j=0;j<I;j++){
  27.                 out.print(matrix[i][j]+"\t");
  28.             }
  29.             out.println();
  30.         }
  31.     }
  32.     public static int Sum(int[][]matrix,int I){
  33.         //Функция находит сумму элементов строки или столбца
  34.         out.println("Should I sum 1.strings or 2.columns? (enter the number of the correct answer)");
  35.         int answer = scan.nextInt();
  36.         int sum = 0;
  37.         while (answer !=1 && answer!=2){
  38.             out.println ("Wrong command, try again");
  39.             out.println("Should I sum 1.strings or 2.columns? (enter the number of the correct answer)");
  40.             answer = scan.nextInt();
  41.         }
  42.         if (answer == 1) sum = SumStrings(matrix,I);
  43.         else if (answer == 2) sum = SumColumns(matrix,I);
  44.         return sum;
  45.     }
  46.     public static int SumStrings(int[][]matrix,int I){
  47.         //Функция находит сумму элементов строки
  48.         int sum=0;
  49.         out.println("Choose number of string, where I should sum the elements (number must be integer, greater than zero and less than matrix size)");
  50.         int N = scan.nextInt();
  51.         N--;
  52.         while (N<0 || N>I){
  53.             out.println ("Wrong number, try again");
  54.             N = scan.nextInt();
  55.         }
  56.         for (int i=0;i<I;i++){
  57.             sum=sum+matrix[N][i];
  58.         }
  59.         return sum;
  60.     }
  61.     public static int SumColumns(int[][]matrix,int I){
  62.         //Функция находит сумму элементов строки
  63.         int sum=0;
  64.         out.println("Choose number of column, where I should sum the elements (number must be integer, greater than zero and less than matrix size)");
  65.         int N = scan.nextInt();
  66.         N--;
  67.         while (N<0 || N>I){
  68.             out.println ("Wrong number, try again");
  69.             N = scan.nextInt();
  70.         }
  71.         for (int i=0;i<I;i++){
  72.             sum=sum+matrix[i][N];
  73.         }
  74.         return sum;
  75.     }
  76.     public static void MainFunction (){
  77.         //Здесь собран весь функционал программы
  78.         out.println("Enter the matrix size (integer number and greater than zero)");
  79.         int I = scan.nextInt();//Считываем размер матрицы
  80.         int[][]matrix = CreateMatrix(I);
  81.         out.println("Original matrix:");
  82.         PrintMatrix(matrix,I);
  83.         int sum = Sum(matrix,I);
  84.         out.println("Sum is"+sum);
  85.  
  86.     }
  87.     public static void main (String [] args){
  88.         //Основная функция, отсюда начинается выполнение приложения
  89.         MainFunction();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement