import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestPaiXu {
public static void main(String[] args) {
System.out.print("请输入二维数组的行和列(整数),用逗号隔开:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
//用来接收从键盘输入的字符串
String s = null;
//存放二维数组的元素个数
int num = 0;
//平均值
int avg = 0;
//一行或一列的和
int sum = 0;
//换位置用的临时变量
int temp = 0;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//用来保存分割字符串后的结果
String[] arr = s.split(",");
int row = Integer.parseInt(arr[0]);
int col = Integer.parseInt(arr[1]);
int[][] superArr = new int[row][col];
num = row * col;
System.out.print("请输入二维数组的" + num + "个元素数据(整数),同样用逗号隔开:");
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
arr = s.split(",");
//判断输入个数是否正确
while(arr.length != num) {
System.out.print("输入元素个数不正确,请重新输入!:");
try {
s = br.readLine();
arr = s.split(",");
} catch (IOException e) {
e.printStackTrace();
}
}
//把分割好的数据,循环放进二维数组,从左到右一行一行的放
for(int i=0; i
for(int j=0; j
int t = Integer.parseInt(arr[row*i+j]);
superArr[i][j] = t;
}
}
System.out.print("请输入一个字符,表示行数或者列数,行数用r表示,列用用c表示:");
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if("r".equals(s)) {
System.out.print("请输入行号1-" + row + ":");
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int inputRowNum = Integer.parseInt(s);
//把每个拿出来求平均值
for(int i=0; i
sum += superArr[inputRowNum-1][i];
}
avg = sum / col;
System.out.println("平均值为:" + avg);
//冒泡升序排列
//第一层for决定一共走几趟
//第二层for循环比较前一个和后一个的大小,如果前一个大于后一个,则交换
for(int j=1; j
for(int i=0; i
if(superArr[inputRowNum-1][i] > superArr[inputRowNum-1][i+1]) {
temp = superArr[inputRowNum-1][i+1];
superArr[inputRowNum-1][i+1] = superArr[inputRowNum-1][i];
superArr[inputRowNum-1][i] = temp;
}
}
}
System.out.print("该行升序排列为:");
for(int i=0; i
System.out.print(superArr[inputRowNum-1][i] + " ");
}
} else {
System.out.print("请输入列号1-" + col + ":");
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int inputColNum = Integer.parseInt(s);
for(int i=0; i
sum += superArr[i][inputColNum-1];
}
avg = sum / row;
System.out.println("平均值为:" + avg);
//冒泡升序排列
for(int j=1; j
for(int i=0; i
if(superArr[i][inputColNum-1] > superArr[i+1][inputColNum-1]) {
temp = superArr[i+1][inputColNum-1];
superArr[i+1][inputColNum-1] = superArr[i][inputColNum-1];
superArr[i][inputColNum-1] = temp;
}
}
}
System.out.print("该列升序排列为:");
for(int i=0; i
System.out.print(superArr[i][inputColNum-1] + " ");
}
}
}
}
弄了毛两个小时,我也算复习一下
4