一个小问题,有情绪的帮忙看看!

hjx_gb2000 2010-03-31 11:26:53
编写程序,要求此程序可以用来读取一个5x5的整数数组,然后显示出每行的求和结果和每列的求和结果。
Enter row 1:8 3 9 0 10
Enter row 2:3 5 17 1 1
Enter row 3:2 8 6 23 1
Enter row 4:15 7 3 2 9
Enter row 5:6 14 2 6 0

Row totals: 30 27 40 36 28
Column totals: 34 37 37 32 21
...全文
187 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
无聊司马 2010-04-01
  • 打赏
  • 举报
回复
最原始的 最简单 请看代码 :
public class Testshuzhu{
public static void main(String[] args){
int []b=new int[5];
int [][]a={{1,2,3,4,5},{6,5,7,8,9},{4,5,6,7,8},{1,2,3,5,6},{5,6,8,9,1}};
int sum=0;
int k;
for(int i=0;i<a.length;i++){
System.out.print("Enter row"+ " " + (i+1) +":");
sum=0;
k=0;

for(int j=0;j<a[i].length;j++){

System.out.print(a[i][j]+" ");
sum+=a[i][j];
k+=a[j][i];
}
System.out.print(sum);
System.out.println("");
b[i]=k;
}
System.out.print(" ");
for(int i=0;i<5;i++)
{
System.out.print(b[i]+" ");

}
}
}
iefus 2010-04-01
  • 打赏
  • 举报
回复
这个还有什么独特方法么
hbgzg3006 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hbgzg3006 的回复:]

Java code

Scanner s=new Scanner(System.in);
int [][]arr=new int[5][5];
int [][]result=new int[2][5];
for(int i=0;i<5;i++){
System.out.print("Enter line "……
[/Quote]
把那个没用的for循环忘了去了!
hbgzg3006 2010-03-31
  • 打赏
  • 举报
回复

Scanner s=new Scanner(System.in);
int [][]arr=new int[5][5];
int [][]result=new int[2][5];
for(int i=0;i<5;i++){
System.out.print("Enter line "+(i+1)+":");
for(int j=0;j<5;j++){
arr[i][j]=s.nextInt();
result[0][i]+=arr[i][j];
result[1][j]+=arr[i][j];
}

}
for(int i=0;i<5;i++){

}
System.out.println("row totals:"+Arrays.toString(result[0]).replaceAll("(\\[|])", ""));
System.out.println("column totals: "+Arrays.toString(result[1]).replaceAll("(\\[|])", ""));
hjx_gb2000 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 j2eemail 的回复:]
无聊帮你写下

Java code

public class ceshi {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] str = {{8……
[/Quote]

我写的跟3楼差不多
hjx_gb2000 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sunyinghao1984 的回复:]
よくできました。
[/Quote]

很独特!!!
sunyinghao1984 2010-03-31
  • 打赏
  • 举报
回复
よくできました。
hjx_gb2000 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 py330316117 的回复:]
这个太基础了 用数组自己写吧 5分钟就能搞定
[/Quote]

嗯,是很简单的,就是想看看大家的实现方法。或者是有没有一些独特的实现
蹭饭熊 2010-03-31
  • 打赏
  • 举报
回复
无聊帮你写下

public class ceshi {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] str = {{8,3,9,0,10},{3,5,17,1,1},{2,8,6,23,1},{15,7,3,2,9},{6,14,2,6,0}};
int[] rowsum = new int[5];
int[] columnsum = new int[5];
for(int i=0;i<str.length;i++){
System.out.print("Enter row: ");
for(int j=0;j<str[i].length;j++){
rowsum[i]+=str[i][j];
System.out.print(str[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<str.length;i++){
for(int j=0;j<str[i].length;j++){
columnsum[i]+=str[j][i];
}
}
System.out.println();
System.out.print("Row totals: ");
for(int i:rowsum){
System.out.print(i+" ");
}
System.out.println();
System.out.print("Column totals: ");
for(int i:columnsum){
System.out.print(i+" ");
}

}

}
py330316117 2010-03-31
  • 打赏
  • 举报
回复
这个太基础了 用数组自己写吧 5分钟就能搞定
LuffySY 2010-03-31
  • 打赏
  • 举报
回复
用个二维数组 保存下很简单的吧
justwalking 2010-03-31
  • 打赏
  • 举报
回复
数组.

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧