Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Уймин Максим
- //IT School, 2 группа
- //09.10.2015
- //Задача 1. Переставить элементы строк/столбцов
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Exersise_1 {
- public static PrintStream out = System.out;
- public static Scanner scan = new Scanner(System.in);
- public static int [][] CreateMatrix (int I){
- //Функция создаёт матрицу по заданным с клавиатуры данным, заполняет её случайными числами
- int matrix[][] = new int [I][];
- for (int i = 0; i<I;i++){
- matrix[i] = new int [I];
- }
- for (int i = 0; i<I;i++){
- for (int j = 0; j<I;j++){
- matrix[i][j]=(-10) + (int)(Math.random()*((10-(-10))+1));
- }
- }
- return matrix;
- }
- public static void PrintMatrix(int[][]matrix,int I){
- //Функция выводит матрицу на экран
- for (int i=0;i<I;i++){
- for (int j=0;j<I;j++){
- out.print(matrix[i][j]+"\t");
- }
- out.println();
- }
- }
- public static int[][] Swap(int[][]matrix,int I){
- //Функция меняет местами элементы строк или столбцов матрицы
- out.println("Should I swap 1.strings or 2.columns? (enter the number of the correct answer)");
- int answer = scan.nextInt();
- while (answer !=1 && answer!=2){
- out.println ("Wrong command, try again");
- out.println("Should I swap 1.strings or 2.columns? (enter the number of the correct answer)");
- answer = scan.nextInt();
- }
- if (answer == 1) matrix = SwapStrings(matrix,I);
- else if (answer == 2)matrix = SwapColumns(matrix,I);
- return matrix;
- }
- public static int[][] SwapStrings(int[][]matrix,int I){
- //Функция меняет местами строки матрицы
- out.println("Choose 2 numbers of strings, what I should swap(number must be integer, greater than zero and less than matrix size)");
- int N1 = scan.nextInt();
- while (N1<0 || N1>I){
- out.println ("Wrong number, try again");
- N1 = scan.nextInt();
- }
- int N2 = scan.nextInt();
- while (N2<0 || N2>I){
- out.println ("Wrong number, try again");
- N2 = scan.nextInt();
- }
- N1--;
- N2--;
- int array[]=new int[I];
- for (int j=0;j<I;j++){
- array[j] = matrix[N1][j];
- matrix[N1][j]=matrix[N2][j];
- matrix[N2][j]=array[j];
- }
- return matrix;
- }
- public static int[][] SwapColumns(int[][]matrix,int I){
- //Функция меняет местами столбцы матрицы
- out.println("Choose 2 numbers of columns, what I should swap(number must be integer, greater than zero and less than matrix size)");
- int N1 = scan.nextInt();
- while (N1<0 || N1>I){
- out.println ("Wrong number, try again");
- N1 = scan.nextInt();
- }
- int N2 = scan.nextInt();
- while (N2<0 || N2>I){
- out.println ("Wrong number, try again");
- N2 = scan.nextInt();
- }
- N1--;
- N2--;
- int array[]=new int[I];
- for (int i=0;i<I;i++){
- array[i] = matrix[i][N2];
- matrix[i][N2] = matrix[i][N1];
- matrix[i][N1] = array[i];
- }
- return matrix;
- }
- public static void MainFunction (){
- //Здесь собран весь функционал программы
- out.println("Enter the matrix size (integer number and greater than zero)");
- int I = scan.nextInt();//Считываем размер матрицы
- int[][]matrix = CreateMatrix(I);
- out.println("Original matrix:");
- PrintMatrix(matrix,I);
- matrix = Swap(matrix,I);
- out.println("Resulting matrix:");
- PrintMatrix(matrix,I);
- }
- public static void main (String [] args){
- //Основная функция, отсюда начинается выполнение приложения
- MainFunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement