日期:2014-05-20 浏览次数:20776 次
public class Demo { public static void main(String[] args) { int[] nums = new int[10]; for(int i = 0; i < 10; i++) { nums[i] = (int) (Math.random() * 999) + 1; } Demo demo = new Demo(); demo.maxMin(nums); for(int i : nums) { System.out.print(i + "\t"); } } public void maxMin(int[] nums) { int max = nums[0]; int min = nums[0]; for(int i : nums) { if(max < i) { max = i; } if(min > i) { min = i; } } System.out.println("最大致为:" + max + ",最小值为:" + min + "."); } }
------解决方案--------------------
public class Test{
public static void maxMin(int[] nums) {
int temp = 0;
int i = 0, j = 0;
for (i = 0; i < nums.length; i++) {
for (j = 0; j < nums.length - 1; j++) {
if (nums[j] < nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
for (int a : nums) {
System.out.print(a + ",");
}
System.out.println();
System.out.println("************************************");
System.out.println("最大值为:" + nums[0]);
System.out.println("最小值为:" + nums[j]);
}
@SuppressWarnings("null")
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[10];
for (int i = 0; i < 10; i++) {
int m = (int) (Math.random() * 1000);
arr[i] = m;
}
Test.maxMin(arr);
}
}
------解决方案--------------------
public class Test{
public static void maxMin(int[] nums) {
int temp = 0;
int i = 0, j = 0;
for (i = 0; i < nums.length; i++) {
for (j = 0; j < nums.length - 1; j++) {
if (nums[j] < nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
for (int a : nums) {
System.out.print(a + ",");
}
System.out.println();
System.out.println("************************************");
System.out.println("最大值为:" + nums[0]);
System.out.println("最小值为:" + nums[j]);
}
@SuppressWarnings("null")
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[10];
for (int i = 0; i < 10; i++) {
int m = (int) (Math.random() * 1000);
arr[i] = m;
}
Test.maxMin(arr);
}
}