import java.util.Comparator; import java.util.Scanner; /** * 现在有一只黑猫黑一只白猫,为了节省猫粮,我们要比较高矮胖瘦然后做出取舍 * 请用面向对象思想来设计,同时要求编码实现程序 * @author 眼镜哥哥爱编程 */ public class TestComparator{ public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("输入白猫高矮:"); int whiteHeigth = s.nextInt(); System.out.print("输入白猫轻重:"); int whiteWeight = s.nextInt(); System.out.print("输入黑猫高矮:"); int blackHeight = s.nextInt(); System.out.print("输入黑猫轻重:"); int blackWeight = s.nextInt(); System.out.println("比较结果:"); Cat catWhite = new Cat(whiteHeigth,whiteWeight); Cat catBlack = new Cat(blackHeight,blackWeight); TestComparator.heightResult(catWhite, catBlack); TestComparator.weightResult(catWhite, catBlack); } public static void heightResult(Cat catWhite,Cat catBlack){ Comparator<Cat> c = new HeightComparator(); catWhite.c = c; catBlack.c = c; int result = catWhite.compareTo(catBlack); if(result == 1){ System.out.println("白猫比黑猫高"); }else if(result == -1){ System.out.println("白猫比黑猫矮"); }else{ System.out.println("白猫与黑猫一边高"); } } public static void weightResult(Cat catWhite,Cat catBlack){ Comparator<Cat> c = new WeightComparator(); catWhite.c = c; catBlack.c = c; int result = catWhite.compareTo(catBlack); if(result == 1){ System.out.println("白猫比黑猫重"); }else if(result == -1){ System.out.println("白猫比黑猫轻"); }else{ System.out.println("白猫与黑猫一样重"); } } } class Cat implements Comparable<Cat>{ public Comparator<Cat> c = null; public int height ; public int weight ; public Cat(){} public Cat(int height,int weight){ this.height = height; this.weight = weight; } public int compareTo(Cat cat){ return c.compare(this,cat); } } class HeightComparator implements Comparator<Cat>{ public int compare(Cat c1,Cat c2){ if(c1.height > c2.height){ return 1; }else if(c1.height < c2.height){ return -1; }else{ return 0; } } } class WeightComparator implements Comparator<Cat>{ public int compare(Cat c1,Cat c2){ if(c1.weight > c2.weight){ return 1; }else if(c1.weight < c2.weight){ return -1; }else{ return 0; } } }
相关推荐
J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API
J2SE_J2EE帮助文档 J2SE_J2EE帮助文档
J2SE_5.0_第05章_数组2.rar
J2SE_5.0_第05章_数组1.rar
J2SE_5.0_第09章_线程1.rar
J2SE_5.0_第05章_数组3.rar
J2SE_5.0_第09章_线程2.rar
J2SE_5.0_第06章_常用类2.rar
J2SE_5.0_第04章_异常处理1.rar
J2SE_5.0_第06章_常用类3.rar
J2SE_5.0_第06章_常用类1.rar
JavaSE_J2SE_5.0_API_中文文档_html格式
J2SE_5.0_第04章_异常处理2.rar
J2SE6.0_Api_CN网页版给大家分享!
J2SE_Java_Api是j2se开发使用的api
JDK中文文档 J2SE_API中文文档由于比较大,压缩成两个文件上传
Hibernate_Api+J2SE_Java_ApiAPI+Servlet&JSP;_Tomcat_Api.rarSpring_Api+Struts_Api.
J2SE_of_ezPuzzle ACG同人拼图小游戏 ============================================= 无聊练习编程时制作的小程序,估计感兴趣的人不多! 不过工作之余放松一下也是很值得的! 【注意】运行此程序需要系统...
JDK中文文档 J2SE_API中文文档(2)第二部分(共两部分)
一份我觉得不错的中文J2SE API文档,拿来给大家一起分享