簡體   English   中英

在數組中使用 java Math 類時出錯

[英]Error in using the java Math class in an array

我正在嘗試使用 java Math 打印最大數量的 2 個數字,但我一直收到錯誤消息。 我是 Java 新手,非常感謝您的幫助。

代碼:

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

錯誤:

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

問題是你也把你的班級稱為Math 編譯器希望在您自己的類中找到方法max

要解決這個問題,請為您的班級命名。

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

或者你可以導入類import java.lang.Math;

java.lang.Math替換Math ,這樣就可以了。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM