java问题求教。 随即产生10个1-1000之间的整数,放在一个数组里,定义方法maxMin(),求出最大值,最小值

要求不鞥使用sort()方法。
public void maxMin(int [] nums)
详细点啊,才学带参方法,不懂啊- --

/**
* 随机从1-1000产生10个整数放到数组里,返回数组里最大值最小值
* @return 最大值最小值
*/
public int[] maxMin(){
//随机产生10个1-1000的整数放到数组nums中
int nums[] = new int[10];
for(int j=0;j<nums.length;j++){
nums[j] = new Random().nextInt(1000)+1;
System.out.println(nums[j]);
}
//找出数组nums中最大值
int max = 0;
for(int i=0;i<nums.length;i++){
if(max<nums[i]){
max = nums[i];
}
}
int min = 1000;
for(int n=0;n<nums.length;n++){
if(min>nums[n]){
min = nums[n];
}
}
int r[] = new int[2];
r[0] = max;
r[1] = min;
System.out.println(r[0]+"|"+r[1]);
return r;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-10-22
import java.util.Random;
public class MAX_RAD{
/*
生成numb个随机数组
*/
public int[] randArray(int numb){
int[]temp_array=new int[numb];
Random radobj=new Random();
for(int i=0;i<numb;i++){
temp_array[i]=radobj. nextInt(1000)+1;
System.out.println(" i="+ temp_array[i]);
}
return temp_array;
}
/*
发现numb个随机数组中的最大值
*/
public int max_Value(int[]array){
int max=array[0];
for(int i=1;i<array.length;i++){
if(max<array[i]){
max=array[i] ;
}
}
return max;
}
/*
发现numb个随机数组中的最小值
*/
public int min_Value(int[]array){
int min=array[0];
for(int j=1;j<array.length;j++){
if(min>array[j]){
min=array[j] ;
}
}
return min;
}
public static void main(String[]agrs){

MAX_RAD maxclass=new MAX_RAD();
int []test_Array=maxclass.randArray(10);
System.out.println("maxvlue="+maxclass.max_Value(test_Array));
System.out.println("minvlue="+maxclass.min_Value(test_Array));
}

}
算法的效率为n
第2个回答  2012-10-11
/*
* @(#)Test.java 1.0 2012-10-11
*
* Copyright (c) 2007-2012 Shanghai Handpay IT, Co., Ltd.
* 16/F, 889 YanAn Road. W., Shanghai, China
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Shanghai Handpay IT Co., Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Handpay.
*/

/**
* Class description goes here.
*
* @version 1.0 2012-10-11
* @author lfjiang
* @history
*
*/
public class Test {

/**
* @author lfjiang 2012-10-11
* @param args
*/
public static void main(String[] args) {
test();
}

public static void test() {
int[] ary = getRandomIntegerAry(10);
int min = getMin(ary);
int max = getMax(ary);
for (int i : ary) {
System.out.print(i+"\t");
}
System.out.println("\nmin:"+min);
System.out.println("max:"+max);
}

/**
* 获取随机数组
* @author lfjiang 2012-10-11
* @return
*/
public static int[] getRandomIntegerAry(int aryLength) {
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0 ; i < aryLength ; i++) {
randoms[i] = random.nextInt(1001);
}
return randoms;
}

/**
* 获取数组最小值
* @author lfjiang 2012-10-11
* @param ary
* @return
*/
public static int getMin(int[] ary){
int min = 0;
if (ary != null && ary.length > 0 ) {
min = ary[0];
for (int i = 0 ; i < ary.length ; i++) {
min = Math.min(ary[i], min);
}
}
return min;
}

/**
* 获取数组最大值
* @author lfjiang 2012-10-11
* @param ary
* @return
*/
public static int getMax(int[] ary) {
int max = 0;
if (ary != null && ary.length > 0 ) {
max = ary[0];
for (int i = 0 ; i < ary.length ; i++) {
max = Math.max(ary[i], max);
}
}
return max;
}

/**
* 取两者最小值
* @author lfjiang 2012-10-11
* @param a
* @param b
* @return
*/
public static int compareMin(int a,int b) {
return a < b ? a : b;
}

/**
* 取两者最大值
* @author lfjiang 2012-10-11
* @param a
* @param b
* @return
*/
public static int compareMax(int a , int b) {
return a > b ? a : b;
}
}
第3个回答  2012-10-11
public class TestHello {
public static void maxMin(int[] nums){
int temp = 0;
for (int i = 0; i < nums.length-1; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(nums[j]>nums[j+1]){
temp = nums[j+1];
nums[j+1] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Max: " + nums[9]);
System.out.println("Min: " + nums[0]);
}
public static void main(String[] args) {
Random r = new Random();
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
array[i] = r.nextInt(1000);
}
TestHello.maxMin(array);
}
}
第4个回答  2012-10-11
public void maxMin(int [] nums){
int a= 0;
for(int i=0;i<nums.length;i++){
if(nums[i]>a){
a = nums[i];
}
}
System.out.println(a);
}
相似回答