杭电oj—— 2054(compareTo)

package com.demo2;

import java.math.BigDecimal;
import java.util.Scanner;

public class HDU_oj2054_2 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		BigDecimal a,b;
		while(sn.hasNextBigDecimal()) {
			a = sn.nextBigDecimal();
			b = sn.nextBigDecimal();
			/* compareTo
			 * 该方法用于两个相同数据类型的比较
			 *  1、如果指定的数与参数相等返回0。
				2、如果指定的数小于参数返回 -1。
				3、如果指定的数大于参数返回 1。
			 */
			if(a.compareTo(b) == 0) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		}
		sn.close();
	}
}

//题目出的不好,数据规模都没写清楚 ,导致我下面写double直接就错了

package com.demo2;
import java.util.Scanner;
/*
 * Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
 */
public class HDU_oj2054 { 
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		Double a, b;
		while (sn.hasNextDouble()) { //输入的是什么,hasNext就是什么
			a = sn.nextDouble();
			b = sn.nextDouble();
			/*
			 * equals()比较的是内容,而==比较的是地址
			 * 两个字符串相比较
			 */
			if (a.toString().equals(b.toString())) {  
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		}
		sn.close();
	}

}

猜你喜欢

转载自blog.csdn.net/LiLi_code/article/details/88554022