
输入 1 2 3 4 5 6 7 8 9 输出 一个三乘三的矩阵 第一层 1 2 3 第二层 4 5 6 第三层 7 8 9
1 2 3 4 5 6 7 8 9 只是例子 其实是9个输入数字 展开
这里1,2,3,4,5,6,7,8,9只是你说的特殊例子,其实这个代码可以支持任意九个数
代码如下:
import java.util.ArrayList;
import java.util.List;
public class BaiduZhiDao {
/**
* @param args
*/
public static void main(String[] args) {
String args0 = args[0];
// String args0 = "1,2,3,4,5,6,7,8,9";
List<List<Double>> ll = new ArrayList<List<Double>>();
String[] argsArr = args0.split(",");
int index = 1;
List<Double> l = new ArrayList<Double>();
for(String str : argsArr) {
l.add(Double.valueOf(str));
if(index % 3 == 0) {
ll.add(l);
l = new ArrayList<Double>();
}
index++;
}
Double[] colSumArr = new Double[]{0d,0d,0d};
int rowIndex = 0;
for(List<Double> list : ll) {
int colIndex = 0;
Double rowSum = 0d;
for(Double d : list) {
colSumArr[colIndex] = getSum(colSumArr[colIndex], d);
rowSum = getSum(rowSum, d);
System.out.print(d + " ");
colIndex++;
}
System.out.print(rowSum);
System.out.println();
rowIndex++;
}
for(Double d : colSumArr) {
System.out.print(d + " ");
}
}
private static Double getSum(Double ovalue, Double nvalue) {
return ovalue + nvalue;
}
}
public static void main(String[] args) {
int[] inputNum = new int[9];
System.out.println("Enter the voltage readings:");
for (int i = 0; i < inputNum.length; i++) {
Scanner input = new Scanner(System.in);
inputNum[i] = input.nextInt();
}
int[] rowSum = new int[3];
int columnSun1 = 0;
int columnSun2 = 0;
int columnSun3 = 0;
System.out.println("The readings are:");
for (int j = 0; j < 9; j = j + 3) {
rowSum[j / 3] = inputNum[j] + inputNum[j + 1] + inputNum[j + 2];
System.out.println(inputNum[j] + "" + inputNum[j + 1] + "" + inputNum[j + 2]);
columnSun1 = columnSun1 + inputNum[j];
columnSun2 = columnSun2 + inputNum[j + 1];
columnSun3 = columnSun3 + inputNum[j + 2];
}
System.out.println("The sum of row 1 is:" + rowSum[0]);
System.out.println("The sum of row 2 is:" + rowSum[1]);
System.out.println("The sum of row 3 is:" + rowSum[2]);
System.out.println("The sum of column 1 is:" + columnSun1);
System.out.println("The sum of column 2 is:" + columnSun2);
System.out.println("The sum of column 3 is:" + columnSun3);
}
测试结果是,
Enter the voltage readings:
1
2
3
4
5
6
7
8
9
The readings are:
123
456
789
The sum of row 1 is:6
The sum of row 2 is:15
The sum of row 3 is:24
The sum of column 1 is:12
The sum of column 2 is:15
The sum of column 3 is:18
我靠。我的百度账号被人偷了。今天刚找回来。看到你的问题还在,就跟你说下。你看看我给你写的代码,放在程序里面跑一跑。是在控制台让你随便输入9个数字。然后回车,他就会显示你想得到的结果!!!!!
嘿嘿 我都毕业了
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
int i=0;
int [] a=new int[10];
int [] row=new int[3];
int [] col=new int[3];
Scanner sc=new Scanner(System.in);
System.out.println("请输入一行数字(以-1结束):");
do
{a[i]=sc.nextInt();
i++;
}while(a[i-1]!=-1);
System.out.println("你输入的矩阵是:");
for(int t=0;t<i-1;t++)
{System.out.print(a[t]+" ");
if ((t+1)%3==0)
System.out.println("");
}
for(int k=0;k<i-1;k++){
row[k/3]+=a[k];
col[k%3]+=a[k];
}
System.out.println("每行之和是:");
for(int m=0;m<3;m++)
System.out.print(row[m]+" ");
System.out.println("");
System.out.println("每列之和是:");
for(int m=0;m<3;m++)
System.out.print(col[m]+" ");
System.out.println("");
}
}