当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Double min()用法及代码示例


Java Double 类的 min() 方法返回两个 double 值中具有次要值的 double。返回的结果与调用 Math.min() 方法相同。

用法

public static double min(double a, double b)

参数

这里,a & b 是要比较的两个操作数。

返回值

min() 方法返回 a 和 b 中较小的值。

例子1

import java.util.Scanner;

public class Double_minMethodExample1 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter first number.");
            Double d1 = scanner.nextDouble();
            System.out.println("Enter second number");
            Double d2 = scanner.nextDouble();
            //return the smaller of two values
            Double d3= Double.min(d1,d2);
            System.out.println("Enter third number");
            Double d4 = scanner.nextDouble();
            Double d5= Double.min(d3,d4);
            System.out.println("The smaller number is:"+d5);
        }
    }

输出:

Enter first number.
67.90
Enter second number
67.89
Enter third number
67.99999
The smaller number is:67.89

例子2

import java.util.Scanner;

    public class Double_minMethodExample2 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter total number of elements");
            int n = scanner.nextInt();
            // creating array
            Double a[] = new Double[n];
            System.out.println("Enter " + n + " elements");
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextDouble();
            }
            Double d1 = 0.0;
            Double d2 =0.0;
            for (int i = 0; i < n - 1; i++) {
                // return the maximum value of n numbers
                d1 = Double.max(a[i], a[i + 1]);
                a[i+1] = d1;
            }
            System.out.println("Maximum value:" + d1);
            for (int i = 0; i < n - 1; i++) {
                // return the minimum value of n numbers
                d2 = Double.min(a[i], a[i+1]);
                a[i+1] = d2;
            }
            System.out.println("Minimum value:"+d2);
        }
    }

输出:

Enter total number of elements
4
Enter 4 elements
100.098
100.9876
19635
100
Maximum value:19635.0
Minimum value:100.098

例子3

import java.util.Scanner;

public class Double_minMethodExample3 {
       public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter number of elements you want.");
            int n = scanner.nextInt();

            Double a[] = new Double[n];
            System.out.println("Enter elements");
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextDouble();
            }
            Double d1, temp = 0.0;
            for (int i = 0; i < n ; i++) {
                for (int j = i+1; j < n; j++) {
                    d1 = Double.min(a[i], a[j]);
                    if (a[i].equals(d1)){
                        temp = d1;
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            System.out.print("Descending order = ");
            for (int i = 0; i < n; i++) {
                System.out.print(a[i]+">");
            }
        }
    }

输出:

Enter number of elements you want.
4
Enter elements
10.000
10,001
11
12
Descending order = 10001.0>12.0>11.0>10.0>

示例 4

public class Double_minMethodExample4 {
       public static void main(String[] args) {
            Double d1=Double.MIN_VALUE;
            System.out.println("Min_Value = "+d1);
            Double d2=Double.MIN_NORMAL;
            System.out.println("Min_Value = "+d2);
            Double d3=Double.min(d1,d2);
            System.out.println("1. Smaller value = "+d3);
            System.out.println();

            Double d7=Double.POSITIVE_INFINITY;
            System.out.println("POSITIVE_INFINITY = "+d7);
            Double d8=Double.NEGATIVE_INFINITY;
            System.out.println("NEGATIVE_INFINITY = "+d8);
            Double d9 =Double.min(d7,d8);
            System.out.println("2. Smaller value = "+d9);
        }
    }

输出:

Min_Value = 4.9E-324
Min_Value = 2.2250738585072014E-308
1. Smaller value = 4.9E-324

POSITIVE_INFINITY = Infinity
NEGATIVE_INFINITY = -Infinity
2. Smaller value = -Infinity






相关用法


注:本文由纯净天空筛选整理自 Java Double min() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。