如何查找数组中的最大值,包括数字

时间:2016-11-18 07:31:24

标签: java arrays input

我必须能够在5长数组中找到最大负值,无论数组是包含负整数还是正整数。我知道这很简单,但如果你引导我朝着正确的方向前进,我将非常感激。

//Import Scanner so we can receive input from the user.
import java.util.Scanner;

public class Mess {

   //Main Method
   public static void main(String[] args) {
      //Declare variables
      int [] list = new int [6];
      int num = 0;
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      int max = 0;

      //While loop to store in array
      while (num<=5){
      list [num]=input.nextInt();
      num++;
   }

   //Arrays.sort(list);
   //For loop to find max value
   //list[num]= max;
   System.out.print(max);System.out.print(max);
   for (int i = 1; i < num; i++) {
      if (list[i] > max){
         max = list[i];
      }
   }      
   //Print out max value
   System.out.print("The max value is " + max + " ");
   input.close();   
   }
}

2 个答案:

答案 0 :(得分:0)

您的开始时import numpy as np from numpy import dtype pad_shape = np.array((3000, 3000, 4)) i = np.zeros((1120, 1472, 4), dtype=np.int) result = np.zeros(pad_shape, dtype=np.int) x, y = 10, 10 shape = i.shape print shape rr = result[y:y+shape[0], x:x+shape[1]] print rr 为零,因此如果数组包含所有负值,则不会触发max语句,并且if不会被覆盖。

max

答案 1 :(得分:0)

最初将max设置为Integer.MIN_VALUE

      //Declare variables
    int [] list = new int [6];
    int num = 0;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int max = Integer.MIN_VALUE;

    //While loop to store in array
    while (num<=5){
        list [num]=input.nextInt();
        num++;
    }

    for (int i = 0; i < num; i++) {
        if (list[i] > max){
           max = list[i];
        }
     }

    //Print out max value
    System.out.print("The max value is " + max + " ");
    input.close();