如何获取Java中的最大和最小数量?

时间:2016-01-14 07:31:09

标签: java

大家好,我是Java的新手。我必须编写一个代码来获取用户投入的最大和最小数字。

这是确切的问题

  

编写一个程序,从命令提示符接受100个实数   并确定最大和最小值。你不被允许   使用“if-else”声明。

     

提示 - 使用Math.max(数字,最大)和Math.min(数字,最小)   方法

这是我写的代码,但我不知道为什么它不会工作

import java.util.Scanner;
public class LargestAndSmallest {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int[] inputtedNumber = new int[20];

        for (int num = 0; num < 20; num += 1) {
            inputtedNumber[num] = in.nextInt();
        }

        int maxNum = 0, minNum = 0;

        for (int checkNum = 0; checkNum < 20; checkNum += 1) {
            maxNum = Math.max(inputtedNumber[checkNum]);
            minNum = Math.min(inputtedNumber[checkNum]);
        }

        System.out.println("The maximum number: " + maxNum);
        System.out.println("The minimum number: " + minNum);
    }
}

我不知道为什么但是我收到此错误消息

maxNum = Math.max(inputtedNumber[checkNum]);
                         ^
    method Math.max(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(double,double) is not applicable
      (actual and formal argument lists differ in length)
error: no suitable method found for min(int)
            minNum = Math.min(inputtedNumber[checkNum]);
                         ^
    method Math.min(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(double,double) is not applicable
      (actual and formal argument lists differ in length)
2 errors

7 个答案:

答案 0 :(得分:5)

根据documentationMath.MaxMath.Min接受两个参数,但是您只提供一个参数。

您需要更改此内容:

    int maxNum = 0, minNum = 0;

    for (int checkNum = 0; checkNum < 20; checkNum += 1) {
        maxNum = Math.max(inputtedNumber[checkNum]);
        minNum = Math.min(inputtedNumber[checkNum]);
    }

对此:

    int maxNum = Integer.MIN_VALUE;
    int minNum = Integer.MAX_VALUE;

    for (int checkNum = 0; checkNum < 20; checkNum += 1) {
        maxNum = Math.max(maxNum, inputtedNumber[checkNum]);
        minNum = Math.min(minNum, inputtedNumber[checkNum]);
    }

如果所有数字都为负数,将maxNum设置为0将导致问题,因为它们都不会大于0.将minNum设置为0会导致问题,如果所有数字都是正数,那么它们都不会低于0。

作为替代方案,您可以将它们都设置为inputtedNumber[0]

答案 1 :(得分:1)

Math.max需要2个整数进行比较,这就是你得到错误的原因。 你可以这样做

import java.util.Scanner;
public class LargestAndSmallest {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int[] inputtedNumber = new int[20];

        for (int num = 0; num < 20; num += 1) {
            inputtedNumber[num] = in.nextInt();
        }

        int maxNum = Integer.MIN_VALUE, minNum = Integer.MAX_VALUE;

        for (int checkNum = 0; checkNum < 20; checkNum += 1) {
            maxNum = Math.max(inputtedNumber[checkNum], maxNum);
            minNum = Math.min(inputtedNumber[checkNum], minNum);
        }

        System.out.println("The maximum number: " + maxNum);
        System.out.println("The minimum number: " + minNum);
    }
}

答案 2 :(得分:1)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        MinMax mm = new MinMax();
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.printf("Geef getal %d: ", i + 1);
            int getal = Integer.parseInt(scanner.nextLine());
            mm.evalueerGetal(getal);


        }
        System.out.printf("Het grootste getal was %d.\n", mm.getMaximum());
        System.out.printf("Het kleinste getal was %d.\n", mm.getMinimum());
    }

}
public class MinMax {

    // private int getal;
    private int minimum = Integer.MAX_VALUE;
    private int maximum = Integer.MIN_VALUE;

    public void evalueerGetal(int getal) {

        if (getal < minimum)
            minimum = getal;
        if (getal > maximum)
            maximum = getal;

    }

    public int getMaximum() {

        return maximum;
    }

    public int getMinimum() {
        return minimum;
    }

}

答案 3 :(得分:0)

方法Math.max采用两个参数并进行比较。您只向该方法传递了一个参数。来自javadoc:

  

返回两个int值中较大的一个。

以下是有关如何使用Math.max

的示例
int first = ...
int second = ...
int max = Math.max(first, second);    

Math.min

也是如此

比较n个数字只需将它们比较为2乘2,例如

int[] inputtedNumber = ...
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int x : inputtedNumber) {
    max = Math.max(x, max);
    min = Math.min(x, min);
}
// Here max has the maximum value of the list
// Here min has the minimum value of the list

答案 4 :(得分:0)

问题是您没有正确使用Math.max方法,请尝试以下代码正确运行

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int[] inputtedNumber = new int[20];

    for (int num = 0; num < 20; num += 1) {
        inputtedNumber[num] = in.nextInt();
    }

    int maxNum = inputtedNumber[0], minNum = inputtedNumber[0];

    for (int checkNum = 0; checkNum < 20; checkNum += 1) {
        maxNum = Math.max(maxNum, inputtedNumber[checkNum]);
        minNum = Math.min(minNum, inputtedNumber[checkNum]);
    }

    System.out.println("The maximum number: " + maxNum);
    System.out.println("The minimum number: " + minNum);
}

答案 5 :(得分:0)

如果您在不使用阵列的情况下编写它,您实际上并不需要关心它是20,50或100万输入数字:

Scanner in = new Scanner(System.in);

int maxNum = Integer.MIN_VALUE;
int minNum = Integer.MAX_VALUE;
while (scanner.hasNextInt()) {
  int num = scanner.nextInt();
  maxNum = Math.max(maxNum, num);
  minNum = Math.min(minNum, num);
}

System.out.println("The maximum number: " + maxNum);
System.out.println("The minimum number: " + minNum);

答案 6 :(得分:-1)

尝试添加第二个参数方法cluster_final = pd.Series() for i in range(len(cluster_6_loc)): cluster_final.set_value(cluster_6_loc.iloc[i], bulk_order_id.values[i]) In [209]: cluster_final Out[209]: Cluster 3 523 dtype: int64 max(...)需要拖曳min(...)个参数:

int