Array中的Java最小值和最大值

我的代码没有给出错误,但它没有显示最小值和最大值。 代码是:

Scanner input = new Scanner(System.in); int array[] = new int[10]; System.out.println("Enter the numbers now."); for (int i = 0; i < array.length; i++) { int next = input.nextInt(); // sentineil that will stop loop when 999 is entered if (next == 999) { break; } array[i] = next; // get biggest number getMaxValue(array); // get smallest number getMinValue(array); } System.out.println("These are the numbers you have entered."); printArray(array); // getting the maximum value public static int getMaxValue(int[] array) { int maxValue = array[0]; for (int i = 1; i  maxValue) { maxValue = array[i]; } } return maxValue; } // getting the miniumum value public static int getMinValue(int[] array) { int minValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] < minValue) { minValue = array[i]; } } return minValue; } //this method prints the elements in an array...... //if this case is true, then that's enough to prove to you that the user input has //been stored in an array!!!!!!! public static void printArray(int arr[]) { int n = arr.length; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } 

我需要system.out.println()来显示它,还是返回工作?

 getMaxValue(array); // get smallest number getMinValue(array); 

您正在调用方法但不使用返回的值。

 System.out.println(getMaxValue(array)); System.out.println(getMinValue(array)); 

您也可以尝试这样做,如果您不想通过您的方法执行此操作。

  Arrays.sort(arr); System.out.println("Min value "+arr[0]); System.out.println("Max value "+arr[arr.length-1]); 

这是找到数组中最小值和最大值的工作代码。我希望你会发现它有用:

  import java.util.Random; import java.util.Scanner; public class FindMin { public static void main(String[] args){ System.out.println("Main Method Started"); Scanner in = new Scanner(System.in); System.out.println("Enter the size of the arr"); int size = in.nextInt(); System.out.println("Enter the maximum value of the arr"); int max = in.nextInt(); int [] arr = initializeArr(max, size); print(arr); findMinMax(arr); System.out.println("Main Method Ended"); } public static void print(int[] arr){ for(int val:arr){ System.out.print(val + " "); } System.out.println(); } public static int[] initializeArr(int max,int size){ Random random = new Random(); int [] arr = new int[size]; for(int ii=0;iimax){ max=arr[ii]; } } System.out.println("The minimum in the arr::"+min); System.out.println("The maximum in the arr::"+max); } } 

Imho最简单的解决方案之一是: –

 //MIN NUMBER Collections.sort(listOfNumbers); listOfNumbers.get(0); //MAX NUMBER Collections.sort(listOfNumbers); Collections.reverse(listOfNumbers); listOfNumbers.get(0); 

你只需丢弃最小值/最大值:

  // get biggest number getMaxValue(array); // <- getMaxValue returns value, which is ignored // get smallest number getMinValue(array); // <- getMinValue returns value, which is ignored as well 

你可以做点什么

  ... array[i] = next; System.out.print("Max value = "); System.out.println(getMaxValue(array)); // <- Print out getMaxValue value System.out.print("Min value = "); System.out.println(getMinValue(array)); // <- Print out getMinValue value ... 

你在这里犯了两个错误。
1.在数组初始化完成之前调用getMaxValue(),getMinValue()方法。
2.不存储getMaxValue(),getMinValue()方法返回的返回值。
所以试试这个代码

  for (int i = 0 ; i < array.length; i++ ) { int next = input.nextInt(); // sentineil that will stop loop when 999 is entered if (next == 999) break; array[i] = next; } // get biggest number int maxValue = getMaxValue(array); System.out.println(maxValue ); // get smallest number int minValue = getMinValue(array); System.out.println(minValue); 

你的最大,最小方法是正确的

但你不打印到控制台的int!

并…可能更好的位置变化(最大,最小)方法

现在(最大,最小)方法在roop中。 它不需要……只需要一个电话

我建议更改此代码

  for (int i = 0 ; i < array.length; i++ ) { int next = input.nextInt(); // sentineil that will stop loop when 999 is entered if (next == 999) break; array[i] = next; } System.out.println("max Value : " + getMaxValue(array)); System.out.println("min Value : " + getMinValue(array)); System.out.println("These are the numbers you have entered."); printArray(array); 

是的,您需要使用System.out.println 。 但是,每次输入值时,您都会获得最小值和最大值,如果它们提前中断,则不会跟踪元素的数量。

尝试:

 for (int i = 0 ; i < array.length; i++ ) { int next = input.nextInt(); // sentineil that will stop loop when 999 is entered if (next == 999) break; array[i] = next; } int length = i; // get biggest number int large = getMaxValue(array, length); // get smallest number int small = getMinValue(array, length); // actually print System.out.println( "Max: " + large + " Min: " + small ); 

然后你必须将长度传递给方法以确定最小值和最大值并打印。 如果不这样做,其余字段将为0并且可能弄乱正确的最小值和最大值。

在这里,您没有打印最大值和最小值。 在getMaxVal和getMin val方法中或在调用之后打印max和min值。 这是输出。

 Enter the numbers now. 5 Max: 5 Min: 0 3 Max: 5 Min: 0 7 Max: 7 Min: 0 3 Max: 7 Min: 0 90 Max: 90 Min: 0 43 Max: 90 Min: 0 100 Max: 100 Min: 0 45 Max: 100 Min: 0 23 Max: 100 Min: 0 22 Max: 100 Min: 3 These are the numbers you have entered. 5 3 7 3 90 43 100 45 23 22 

同样,当您声明一个数组时,它最初都是0。

 import java.util.*; class Maxmin { public static void main(String args[]) { int[] arr = new int[10]; Scanner in = new Scanner(System.in); int i, min=0, max=0; for(i=0; i<=arr.length; i++) { System.out.print("Enter any number: "); arr[i] = in.nextInt(); } min = arr[0]; for(i=0; i<=9; i++) { if(arr[i] > max) { max = arr[i]; } if(arr[i] < min) { min = arr[i]; } } System.out.println("Maximum is: " + max); System.out.println("Minimum is: " + min); } } 

//在数组中查找Max和Min值而不在java中进行排序

 import java.util.Scanner; import java.util.*; public class MaxMin_WoutSort { public static void main(String args[]) { int n,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE; System.out.println("Enter the number of elements: "); Scanner sc = new Scanner(System.in); int[] arr = new int[sc.nextInt()]; //U can't say static or dynamic. //UnWrapping object sc to int value;sc.nextInt() System.out.println("Enter the elements: "); for(int i=0;imax) //Maximum Condition max = arr[j]; else if(arr[j] 

我已更新相同的代码,请将代码与您的原始代码进行比较:

 public class Help { public static void main(String args[]){ Scanner input = new Scanner(System.in); int array[] = new int[10]; System.out.println("Enter the numbers now."); for (int i = 0; i < array.length; i++) { int next = input.nextInt(); // sentineil that will stop loop when 999 is entered if (next == 999) { break; } array[i] = next; } System.out.println("These are the numbers you have entered."); printArray(array); // get biggest number System.out.println("Maximum: "+getMaxValue(array)); // get smallest number System.out.println("Minimum: "+getMinValue(array)); } // getting the maximum value public static int getMaxValue(int[] array) { int maxValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > maxValue) { maxValue = array[i]; } } return maxValue; } // getting the miniumum value public static int getMinValue(int[] array) { int minValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] < minValue) { minValue = array[i]; } } return minValue; } //this method prints the elements in an array...... //if this case is true, then that's enough to prove to you that the user input has //been stored in an array!!!!!!! public static void printArray(int arr[]) { int n = arr.length; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }