shiyan9


1. 打乱行
编写一个方法打乱一个二维整型数组的行:
public static void shuffle(int[][] m)
编写一个测试程序,打乱下面的矩阵:
Int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};

 

package com.cust.daluan;

import java.util.Scanner;

 

public class test {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);


int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
while (true){
System.out.println("原来的数组是");
for(int i=0;i<5;i++){
for(int j=0;j<2;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
System.out.println("输入要打断的行a b");
int a = scan.nextInt()-1;
int b = scan.nextInt()-1;
tools t=new tools();
t.mix(m, a, b);
System.out.println("打乱之后是");
for(int i=0;i<5;i++){
for(int j=0;j<2;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}

}

 

 

 

 

package com.cust.daluan;

public class tools {
public void mix(int[][] m,int a,int b){
int hang= m.length;
int lie=m[0].length;
if(a+1>hang||b+1>hang){
System.out.println("error");

}else{
int[] temp=new int[lie];
temp=m[a];
m[a]=m[b];
m[b]=temp;
}

}

 

}

posted on 2016-11-21 10:18  天才程序猿  阅读(275)  评论(0编辑  收藏  举报

导航