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


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


Java Double 类的 equals() 方法将此 Double 对象与指定的 Double 对象进行比较。

如果参数不为空,则该方法返回 true。

假设 d1 和 d2 是 Double 类的两个实例,则 d1.equals(d2) 的值将是 true,如果

d1.doubleValue() == d2.doubleValue ()

还返回值 true。

覆盖:

Double 类的 equals() 方法覆盖了 Object 类中的 equals() 方法。

用法

public boolean equals(Object obj)

参数

obj -这是要比较的对象。

返回值

如果对象相同,equals() 方法将返回 true,否则返回 false。

例子1

import java.util.Scanner;

public class Double_equalsMethodExample1 {
    public static void main(String[] args) {

        Double d1 = new Double(12345.89);

        Scanner scanner= new Scanner(System.in);
        System.out.println("Enter your bank accountNo.");
        Double d2= scanner.nextDouble();

        if(d2.equals(d1)){

            System.out.println("Your accountNo has matched.Now you can proceed with your transaction");
        }
        else
        {
            System.err.println("Incorrect bank accountNo");
        }
    }
}

输出:

Enter your bank accountNo.
12345.89
Your accountNo has matched.Now you can proceed with your transaction

例子2

import java.util.Scanner;



public class Double_equalsMethodExample2 {



    public static void main(String[] args) {



        Double d1 = new Double(2);

        System.err.println("Q. If an ant has 4 legs.How many legs do 8 ants will have ?\n1.8 Legs \b  2.32 legs \b  3.65legs");

        Scanner scanner= new Scanner(System.in);

        System.out.println("Enter your option ");
		        Double d2 = scanner.nextDouble();



        if(d2.equals(d1)){

            System.out.println("Your answer is correct!");

        }

        else{

            System.out.println("opps! Wrong answer. Try again");

        }

    }

}

输出:

Q. If an ant has 4 legs.How many legs do 8 ants will have ?
1.8 Legs 	  2.32 legs 	  3.65legs
Enter your option 
2
Your answer is correct!

例子3

import java.util.Scanner;



public class Double_equalsMethodExample3 {

    public static void main(String[] args) {

        Double d1 = new Double(768.90);

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your name.");

        String str = scanner.next();



        System.out.println("Enter your old password.");

        Double d2 = scanner.nextDouble();





        if (d1.equals(d2)) {



            System.out.println("Enter your new password");

            String str2 = scanner.next();



            System.out.println(str+" your password has successfully changed to " + str2);

        } else {

            System.out.println("Oops! wrong password. Re-enter the correct password");

        }



    }

}

输出:

Enter your name.
Himanshu
Enter your old password.
768.90
Enter your new password
[email protected]
Himanshu your password has successfully changed to [email protected]






相关用法


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