2016-12-24 56 views
0
import java.util.Scanner; 

/** 
* 
* @author JEEWAT RAM 
*/ 
public class Task5lab8 { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

     Scanner in = new Scanner(System.in); 
     System.out.println("enter range of numbers you want in array:"); 

     int a = in.nextInt(); 
     int[] numbers = new int[a]; 
     System.out.println("enter all numbers: "); 
     for (int i = 0; i < a; i++) { 

      numbers[i] = in.nextInt(); 

     } 
    } 
} 
+0

什么是你的问题吗? –

回答

0

只要您可以使用条件如下:

//first we will consider that our min value is the first on in the array 
int min = numbers[0]; 
//check every element with this min 
for (Integer n : numbers) { 
    //if this value in less then the first value change the min with this value 
    if (min > n) { 
     min = n; 
    } 
} 

同样的事情,最大:

int max = numbers[0]; 
for (Integer n : numbers) { 
    if (max < n) { 
     max = n; 
    } 
} 
0
int max = numbers[0]; 
int min = max; 

for (int n : numbers) { 
    if (n > max) { 
     max = n; 
    } 
    if (n < min) { 
     min = n; 
    } 
} 
+0

不客气 – davidxxx

相关问题