简体   繁体   中英

Error in using the java Math class in an array

I'm trying to print the largest number of 2 numbers using java Math but I keep getting an error. I'm new to Java and I would really appreciate the help.

Code:

public class Math {
  public static void main(String[] args) {
    System.out.println(Math.max(5, 10));  
  }
}

Error:

Math.java:3: error: cannot find symbol
    System.out.println(Math.max(5, 10));
                           ^
  symbol:   method max(int,int)
  location: class Math
1 error

The problem is that you called your class Math as well. The compiler wants to find the method max in your own class.

To fix that, give your class another name.

public class Math {
  public static void main(String[] args) {
    System.out.println(java.lang.Math.max(5, 10));  
  }
}

or you can import the class import java.lang.Math;

Replace Math with java.lang.Math and this would work.

class Math {
    public static void main(String[] args) {
        System.out.println(java.lang.Math.max(5, 10));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM