2013-08-30 77 views
10

Mã của tôi không đưa ra lỗi, tuy nhiên nó không hiển thị giá trị tối thiểu và tối đa. Mã này là:Giá trị tối thiểu và tối đa của Java trong Array

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 < 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] + " "); 
    } 
} 

Tôi có cần system.out.println() để hiển thị hoặc công việc trả lại không?

+0

Tất nhiên bạn phải in các số. Tốt hơn là nơi bạn gọi nó. Và gọi hai phương thức đó ngoài vòng lặp for. –

Trả lời

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

Bạn đang gọi phương thức nhưng không sử dụng giá trị trả lại.

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

Cảm ơn bạn! Đúng thứ tôi cần! – user2673161

3

Bạn chỉ cần vứt bỏ giá trị Min/Max:

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

Bạn có thể làm một cái gì đó giống như

... 
    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 

    ... 
3

Bạn đang làm hai sai lầm ở đây.
1. gọi getMaxValue(),getMinValue() phương thức trước khi khởi tạo mảng hoàn tất.
2.Không lưu trữ giá trị trả lại được trả về theo phương thức getMaxValue(),getMinValue().
Vì vậy, thử mã này

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); 
1

tối đa, phương pháp tối thiểu là đúng

nhưng bạn không in int để an ủi!

và ... có thể thay đổi vị trí tốt hơn (tối đa, tối thiểu) phương pháp

hiện tại (tối đa, tối thiểu) phương pháp trong vòng. nó được cần không .. chỉ cần một cuộc gọi

tôi đề nghị thay đổi mã này

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); 
1

Có bạn cần phải sử dụng một System.out.println. Nhưng bạn đang nhận được tối thiểu và tối đa mỗi khi họ nhập vào một giá trị và không theo dõi số lượng các yếu tố nếu họ phá vỡ sớm.

Hãy thử:

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); 

Sau đó, bạn sẽ phải vượt qua chiều dài vào các phương pháp để xác định min và max và in. Nếu bạn không làm điều này, phần còn lại của trường sẽ là 0 và có thể làm hỏng các giá trị tối thiểu và tối đa thích hợp.

11

Bạn cũng có thể thử điều này, nếu bạn không muốn thực hiện điều này theo phương pháp của mình.

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

Phân loại là O (n * log (n)), việc tìm giá trị min/max là O (n). –

1

Tại đây bạn chưa in giá trị tối đa và tối thiểu. In các giá trị max và min trong phương thức getMaxVal và getMin val hoặc sau cuộc gọi. Đây là đầu ra.

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 

Also when you are declaring an array, it has all 0s initially.

4

IMHO một trong những giải pháp đơn giản nhất là: -

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

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

Dưới đây là đoạn code làm việc để tìm min và max trong mảng.Tôi hy vọng bạn sẽ tìm thấy nó hữu ích:

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;ii<arr.length;ii++){ 
      arr[ii]=random.nextInt(max); 
     } 
     return arr; 
    } 
    public static void findMinMax(int[] arr){ 
     int min=arr[0]; 
     int max=arr[0]; 
     for(int ii=0;ii<arr.length;ii++){ 
      if(arr[ii]<min){ 
       min=arr[ii]; 
      } 
      else if(arr[ii]>max){ 
       max=arr[ii]; 
      } 
     } 
     System.out.println("The minimum in the arr::"+min); 
     System.out.println("The maximum in the arr::"+max); 
    } 
} 
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); 
    } 
} 
+1

Nó không hoạt động vì nó đưa ra lỗi này: Ngoại lệ trong chủ đề "chính" java.lang.ArrayIndexOutOfBoundsException –

1

// Để Tìm Max và Min giá trị trong một mảng mà không cần sắp xếp trong 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;i<arr.length;i++)  //Loop for entering values in array 
     { 
      int next = sc.nextInt(); 
      arr[i] = next; 
     } 
     for(int j=0;j<arr.length;j++) 
    { 
      if(arr[j]>max)    //Maximum Condition 
      max = arr[j]; 
      else if(arr[j]<min)   //Minimum Condition 
       min = arr[j]; 
    } 
    System.out.println("Highest Value in array: " +max); 
    System.out.println("Smallest Value in array: "+min); 

} 
} 
0

Tôi đã cập nhật mã tương tự bạn hãy so sánh mã với mã ban đầu của bạn:

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] + " "); 
    } 
} 
}