输入 1 2 3 4 5 6 7 8 9 输出 一个三乘三的矩阵 第一层 1 2 3 第二层 4 5 6 第三层 7 8 9

输入123456789输出一个三乘三的矩阵第一层123第二层456第三层789还有3*3矩阵每行的和每列的和123456789只是例子其实是9个输入数字... 输入 1 2 3 4 5 6 7 8 9 输出 一个三乘三的矩阵 第一层 1 2 3 第二层 4 5 6 第三层 7 8 9 还有 3*3矩阵 每行的和 每列的和
1 2 3 4 5 6 7 8 9 只是例子 其实是9个输入数字
展开
 我来答
644792799
2012-04-06 · TA获得超过662个赞
知道小有建树答主
回答量:696
采纳率:100%
帮助的人:568万
展开全部

这里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;

}

}

wwyleoej
2012-03-21
知道答主
回答量:1
采纳率:0%
帮助的人:1745
展开全部
输出代码第一行1第二行为2,3第三行为3,4,5第四行为4,5,6,7第用俩个for循环就搞定了嘛! #include <stdio.h> void main() { for( ,
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
蚂蚁狂飙
2012-05-08 · 超过23用户采纳过TA的回答
知道答主
回答量:94
采纳率:0%
帮助的人:39.9万
展开全部
我自己刚写的代码。
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个数字。然后回车,他就会显示你想得到的结果!!!!!
追问
嘿嘿 我都毕业了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友8171f88
2012-03-28
知道答主
回答量:18
采纳率:0%
帮助的人:11.4万
展开全部
import java.util.*;

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("");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
eniper
2012-03-21 · 超过11用户采纳过TA的回答
知道答主
回答量:75
采纳率:0%
帮助的人:50.4万
展开全部
这个应该很简单啊 这个就是对嵌套循环的 一点应用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(8)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式