「Java」Mathクラスのmax、min、abs関数の使い方

2021年2月15日

書式
1.public static int max(int a,int b)
2つのint値のうち大きいほうを返します

2.public static int min(int a,int b)
2つのint値のうち小さいほうを返します。

3.public static double abs(double a)
double値の絶対値を返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.tm;
import static java.lang.Math.*;
public class MathSample {
public static void main(String[] args) {
int x = 15, y = 25, z = 5;
double k = -23.56;
int result = max(x, y);
System.out.println("結果(max関数):" + result);
int res = min(x, z);
System.out.println("結果(min関数):" + res);
double res2 = abs(k);
System.out.println("結果(abs関数):" + res2);
}
}
package com.arkgame.study.tm; import static java.lang.Math.*; public class MathSample { public static void main(String[] args) { int x = 15, y = 25, z = 5; double k = -23.56; int result = max(x, y); System.out.println("結果(max関数):" + result); int res = min(x, z); System.out.println("結果(min関数):" + res); double res2 = abs(k); System.out.println("結果(abs関数):" + res2); } }
package com.arkgame.study.tm;

import static java.lang.Math.*;

public class MathSample {

      public static void main(String[] args) {

            int x = 15, y = 25, z = 5;
            double k = -23.56;
            int result = max(x, y);
            System.out.println("結果(max関数):" + result);

            int res = min(x, z);
            System.out.println("結果(min関数):" + res);

            double res2 = abs(k);
            System.out.println("結果(abs関数):" + res2);

      }

}

実行結果
結果(max関数):25
結果(min関数):5
結果(abs関数):23.56

Java

Posted by arkgame