用JAVA语言编程.有一个5×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号.
答案:5 悬赏:40 手机版
解决时间 2021-03-17 03:45
- 提问者网友:蓝琪梦莎
- 2021-03-16 22:19
用JAVA语言编程.有一个5×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号.
最佳答案
- 五星知识达人网友:琴狂剑也妄
- 2021-03-16 23:33
import java.util.Random;
public class Test2 {
public static void main(String[] args){
// 初始化出一个5X4的矩阵
int[][] a = new int[5][4];
Random r = new Random();
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = r.nextInt(100);
System.out.print(a[i][j] + " ");
}
System.out.println();
}
// 找最大值
int max = a[0][0];
int maxi = 0;
int maxj = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
System.out.println("max value:" + max + " max index:[" + maxi +"]["+ maxj+"]");
}
}
public class Test2 {
public static void main(String[] args){
// 初始化出一个5X4的矩阵
int[][] a = new int[5][4];
Random r = new Random();
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = r.nextInt(100);
System.out.print(a[i][j] + " ");
}
System.out.println();
}
// 找最大值
int max = a[0][0];
int maxi = 0;
int maxj = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
System.out.println("max value:" + max + " max index:[" + maxi +"]["+ maxj+"]");
}
}
全部回答
- 1楼网友:愁杀梦里人
- 2021-03-17 04:13
int a[][] = new int[5][4];
//a赋值略
int max = a[0][0];
int x = 0;
int y = 0;
for(int i=0;i for(int j=0;i if(max < a[i][j]){
max = a[i][j];
x = i;
y = j;
}
}
}
System.out.println("最大值为:" + max + "坐标为:"+x+":"+y);
//a赋值略
int max = a[0][0];
int x = 0;
int y = 0;
for(int i=0;i for(int j=0;i if(max < a[i][j]){
max = a[i][j];
x = i;
y = j;
}
}
}
System.out.println("最大值为:" + max + "坐标为:"+x+":"+y);
- 2楼网友:山河有幸埋战骨
- 2021-03-17 03:57
int x = 0;
int y = 0;
int max = 0;
int[][] a = new int[5][4];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = ((int)(1+Math.random()*100000));
System.out.println("第"+i+"行 " + j +"列值为:"+ a[i][j]);
if(a[i][j] > max){
max = a[i][j] ;
x = i;
y = j;
}
}
}
System.out.println("最大值为:"+max +"在第"+x+"行"+y+"列");
int y = 0;
int max = 0;
int[][] a = new int[5][4];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = ((int)(1+Math.random()*100000));
System.out.println("第"+i+"行 " + j +"列值为:"+ a[i][j]);
if(a[i][j] > max){
max = a[i][j] ;
x = i;
y = j;
}
}
}
System.out.println("最大值为:"+max +"在第"+x+"行"+y+"列");
- 3楼网友:天凉才是好个秋
- 2021-03-17 02:45
public class Test {
public static void main(String[] args){
//定义矩阵
int[][] a =new int[][]{{1,2,3,4},{12,22,3388,44},{51,62,3776,4},{18,2,73,4},{1,26,35,448}};
//记录行好和列号
int h=0;
int l=0;
for(int i=0;i<5;i++){
for(int j=0;j<4;j++){
if(a[h][l] h=i;
l=j;
}
}
}
//因为Java中的数组索引时从0开始的,与数学中矩阵不一样,需要加1
System.out.println("最大值:"+a[h][l]+" 行号为"+(h+1)+" 列号为"+(l+1));
}
}
public static void main(String[] args){
//定义矩阵
int[][] a =new int[][]{{1,2,3,4},{12,22,3388,44},{51,62,3776,4},{18,2,73,4},{1,26,35,448}};
//记录行好和列号
int h=0;
int l=0;
for(int i=0;i<5;i++){
for(int j=0;j<4;j++){
if(a[h][l] h=i;
l=j;
}
}
}
//因为Java中的数组索引时从0开始的,与数学中矩阵不一样,需要加1
System.out.println("最大值:"+a[h][l]+" 行号为"+(h+1)+" 列号为"+(l+1));
}
}
- 4楼网友:夜余生
- 2021-03-17 01:11
public class MaxInMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr={{1,3,4,5},{3,2,5,23},{324,54,323,32},{232,343,2,3212},{1,4,5,3430}};
int max=arr[0][0];
int col=0;
int row=0;
for(int i=0;i<5;i++)
for(int j=0;j<4;j++){
if(arr[i][j]>max){
max=arr[i][j];
col=j+1;
row=i+1;
}
}
System.out.println("最大:"+max+"行:"+row+"列:"+col);
}
随便写的,你可以改造一下
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr={{1,3,4,5},{3,2,5,23},{324,54,323,32},{232,343,2,3212},{1,4,5,3430}};
int max=arr[0][0];
int col=0;
int row=0;
for(int i=0;i<5;i++)
for(int j=0;j<4;j++){
if(arr[i][j]>max){
max=arr[i][j];
col=j+1;
row=i+1;
}
}
System.out.println("最大:"+max+"行:"+row+"列:"+col);
}
随便写的,你可以改造一下
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯