Advertisement
Uimin_Maxim

Задача 1

Jan 27th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. //Уймин Максим
  2. //IT School, 2 группа
  3. //09.10.2015
  4. //Задача 1. Переставить элементы строк/столбцов
  5. import java.io.PrintStream;
  6. import java.util.Scanner;
  7. public class Exersise_1 {
  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[][] Swap(int[][]matrix,int I){
  33.         //Функция меняет местами элементы строк или столбцов матрицы
  34.         out.println("Should I swap 1.strings or 2.columns? (enter the number of the correct answer)");
  35.         int answer = scan.nextInt();
  36.         while (answer !=1 && answer!=2){
  37.             out.println ("Wrong command, try again");
  38.             out.println("Should I swap 1.strings or 2.columns? (enter the number of the correct answer)");
  39.             answer = scan.nextInt();
  40.         }
  41.         if (answer == 1) matrix = SwapStrings(matrix,I);
  42.         else if (answer == 2)matrix = SwapColumns(matrix,I);
  43.         return matrix;
  44.     }
  45.     public static int[][] SwapStrings(int[][]matrix,int I){
  46.         //Функция меняет местами строки матрицы
  47.         out.println("Choose 2 numbers of strings, what I should swap(number must be integer, greater than zero and less than matrix size)");
  48.         int N1 = scan.nextInt();
  49.         while (N1<0 || N1>I){
  50.             out.println ("Wrong number, try again");
  51.             N1 = scan.nextInt();
  52.         }
  53.         int N2 = scan.nextInt();
  54.         while (N2<0 || N2>I){
  55.             out.println ("Wrong number, try again");
  56.             N2 = scan.nextInt();
  57.         }
  58.         N1--;
  59.         N2--;
  60.         int array[]=new int[I];
  61.         for (int j=0;j<I;j++){
  62.             array[j] = matrix[N1][j];
  63.             matrix[N1][j]=matrix[N2][j];
  64.             matrix[N2][j]=array[j];
  65.         }
  66.         return matrix; 
  67.     }
  68.     public static int[][] SwapColumns(int[][]matrix,int I){
  69.         //Функция меняет местами столбцы матрицы
  70.         out.println("Choose 2 numbers of columns, what I should swap(number must be integer, greater than zero and less than matrix size)");
  71.         int N1 = scan.nextInt();
  72.         while (N1<0 || N1>I){
  73.             out.println ("Wrong number, try again");
  74.             N1 = scan.nextInt();
  75.         }
  76.         int N2 = scan.nextInt();
  77.         while (N2<0 || N2>I){
  78.             out.println ("Wrong number, try again");
  79.             N2 = scan.nextInt();
  80.         }
  81.         N1--;
  82.         N2--;
  83.         int array[]=new int[I];
  84.         for (int i=0;i<I;i++){
  85.             array[i] = matrix[i][N2];
  86.             matrix[i][N2] = matrix[i][N1];
  87.             matrix[i][N1] = array[i];
  88.         }
  89.         return matrix;
  90.     }
  91.     public static void MainFunction (){
  92.         //Здесь собран весь функционал программы
  93.         out.println("Enter the matrix size (integer number and greater than zero)");
  94.         int I = scan.nextInt();//Считываем размер матрицы
  95.         int[][]matrix = CreateMatrix(I);
  96.         out.println("Original matrix:");
  97.         PrintMatrix(matrix,I);
  98.         matrix = Swap(matrix,I);
  99.         out.println("Resulting matrix:");
  100.         PrintMatrix(matrix,I);
  101.     }
  102.     public static void main (String [] args){
  103.         //Основная функция, отсюда начинается выполнение приложения
  104.         MainFunction();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement