如何从用户输入的数组中显示最大和最小的数字?

时间:2015-01-03 14:02:54

标签: java arrays

有人可以帮我解决我的代码有什么问题我在显示数组中最大和最小的部分时遇到了麻烦。对不起,我是java的新手。非常感谢

package problem6;
import java.util.Scanner;
public class Problem6 {

    public static void main(String[] args) {
   int input;
   int min = 0;
   int max = 0;
    Scanner keyboard = new Scanner(System.in);   
    System.out.println("How many numbers do you want to enter?");
    input = keyboard.nextInt();

    int array[] = new int[input]; 

        for (int i = 0 ; i < array.length; i++ ) {
           System.out.println("Enter number: ");
           array[i] = keyboard.nextInt(); 
        }  

        {   
            if (input > max)   
            {   
                max = input;  
            } 
            else if (input <= min)   
            {   
                min = input;   
            }     
        }   
        System.out.print("\nLargest: " + max);   
        System.out.print("\nSmallest:" + min); 
    }   
}     

4 个答案:

答案 0 :(得分:2)

发布读取值后,需要迭代数组并尝试使用混合/最大比较逻辑,如:

int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;    
for (int number : array) {//use separate for loop or use the same for loop to which you add numbers in array.
    if (number > max) {   
        max = input;  
    } 
    else if (number < min) {   
       min = input;   
    }     
}

使用相同的for循环:

int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE; 
for (int i = 0 ; i < array.length; i++ ) {
    System.out.println("Enter number: ");
    array[i] = keyboard.nextInt(); 
    if (array[i] > max) {   
        max = array[i];  
    } 
    else if (array[i] < min)   
    {   
       min = array[i];   
    }     
}   

答案 1 :(得分:1)

错误是

  1. 一对额外的大括号,和2.比较输入大小而不是数组元素与min和max。
  2. 可能的工作代码如下所示: -

    package problem6;
    import java.util.Scanner;
    public class Problem6 {
    
        public static void main(String[] args) {
       int input;
       int min = 0;
       int max = 0;
        Scanner keyboard = new Scanner(System.in);   
        System.out.println("How many numbers do you want to enter?");
        input = keyboard.nextInt();
    
        int array[] = new int[input]; 
    
            for (int i = 0 ; i < array.length; i++ ) {
               System.out.println("Enter number: ");
               array[i] = keyboard.nextInt();
    
                if (array[i] >= max)   
                {   
                    max = array[i];  
                } 
                else if (array[i] <= min)   
                {   
                    min = array[i];   
                }     
            }   
            System.out.print("\nLargest: " + max);   
            System.out.print("\nSmallest:" + min); 
        }   
    }     
    

答案 2 :(得分:0)

您需要拥有以下代码:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
    for (int i = 0 ; i < array.length; i++ ) {
                if (array[i]> max)   
                {   
                    max = array[i];  
                } 
                else if (array[i] <= min)   
                {   
                    min = array[i];   
                }     
            }   
            System.out.print("\nLargest: " + max);   
            System.out.print("\nSmallest:" + min);

答案 3 :(得分:0)

你甚至可以试试这个。

int max=array[0], min=array[0];

for(int x=0; x<array.length; x++){
    max = array[x]>max?array[x]:max;        
    min = array[x]<min?array[x]:min;
}

将第一个数组元素设置为min和max first。