java编写一个方法,在方法里面实现嵌套的循环(可以用for或者while),返回循环执行的最大次数.方法的参数是一

2024-11-27 04:35:38
推荐回答(5个)
回答1:

public class exercise {

public static void main(String[] args) {

int key= 2;

int[][] arr = {{1,2},{1,2,3},{2,4,6},{2,2,3}};

Array(key,arr);//这里定义Array方法调用KEY值和数组

}

private static void Array(int key ,int a[][]){

int count=0;

for(int i=0;i
for(int j=0;j
if(a[i][j] ==key){

count++;//如有相同的数字则COUNT加一
}

}
}
System.out.print("共有"+count+"个值与KEY值相同");
}

}

回答2:

public class TestArray {
public static void main (String[] args) {
int key = 2; //给key一个初始值;
int[][] arr = {{1,2},{1,2,3},{2,4,6},{2,2,3}}; //初始化二维数组;
int num = 0; //记录找到与key相等值的个数;
for(int i=0; i for(int j=0; j if(arr[i][j] == key) {
num++;
}
}
}
System.out.println(num); //输出num值;
}
}

回答3:

/*定义一个整形的数组,长度为10,任意赋值,用比较排序法将数组排序;
*/
public class E {
public static void main(String[] args) {
int [] a={1,9,6,4,3,8,2,7,5,10};
for(int i=0;i for(int j=0;j if(a[i]>a[j]){
int zhang=a[i];
a[i]=a[j];
a[j]=zhang;
}
}
}
for(int i=0;i System.out.println(a[i]);
}
}

}

回答4:

1楼已经写得差不多了,只是没有单独写一个方法。呃,我也写些吧
public static int[][] test = new int[][]{{1,2,3},{4,5,6},{3,5,9},{1,3,7},{10,3,8}};
public static void main(String[] args){
System.out.println(findKey(3));
}

public static int findKey(int key){
int count = 0;

for(int[] a : test){
for(int b : a){
count += (key == b) ? 1 : 0;
}
}
return count;
}

回答5:

public static int countNum(int key,int[][] srcDataArr){
int count = 0;
int[][] ab = srcDataArr;
for(int i=0;i for(int j=0;j System.out.println( "i="+i+"\t j="+j+"\t"+ab[i][j] );
if( ab[i][j]==key ){
count++;
}
}
}
System.out.println( count );
return count;
}
public static int[][] constructTwoDimArray(int firstDimLen,int secondDimLen){
Random random = new Random();
int[][] ab = new int[firstDimLen][secondDimLen];
for(int i=0;i for(int j=0;j ab[i][j] = random.nextInt(10);
}
}
return ab;
}
public static void main(String[] args)throws Exception{
int[][] ab = constructTwoDimArray(5,5);
countNum( 4,ab );
}