利用实现接口Comparator的类,重写compare(To1, To2)方法,以此作为TreeMap的排序规则。即按照TreeMap的构造方法public TreeMap(Comparator super K>comparator)来创建映像树TreeMap的对象。达到建立有序的映像树结构。存入7名同学的信息(学号、姓名、身高、体重、Java成绩、数据结构成绩,可以有相同的姓名)。要求实现按照姓名+身高+总成绩三个属性由大到小排序(即总成绩相同时按姓名由大到小排序,总成绩和姓名都相同情况下按身高由大到小排序)。import static java.lang.System.*;import java.util.*;//CompareCom实现接口Comparator中的方法compare,//按照Student对象的name,height和score域的从小到大排序class CompareCom implements Comparator{public int compare(String s1, String s2){【代码1】//添加代码,实现compare方法}}class Student{String name=null;int no,height,weight;float score;Student(int n,String name,int h,int w,float sc){no=n;this.name=name;height=h;weight=w;score=sc;}}public class Example7_14xitiX{public static void main(String args[]){Student st1,st2,st3,st4,st5,st6,st7;st1=new Student(1,"zhanying",182,80,95.5f);st2=new Student(2,"wangheng",170,70,98f);st3=new Student(3,"Liuqing",175,60,78f);st4=new Student(4,"zhanying",172,80,85.5f);st5=new Student(5,"wanheng",165,70,92f);st6=new Student(6,"wuquan",162,60,78f);st7=new Student(7,"Lufen",175,60,98.5f);CompareCom compareCom=new CompareCom();//排序树元素与定制的排序方式(按姓名+身高+成绩排序)关联TreeMaptree=new TreeMap(compareCom);tree.put(st1.name+Integer.toString(st1.height)+Float.toString(st1.score),st1);tree.put(st2.name+Integer.toString(st2.height)+Float.toString(st2.score),st2);tree.put(st3.name+Integer.toString(st3.height)+Float.toString(st3.score),st3);tree.put(st4.name+Integer.toString(st4.height)+Float.toString(st4.score),st4);tree.put(st5.name+Integer.toString(st5.height)+Float.toString(st5.score),st5);tree.put(st6.name+Integer.toString(st6.height)+Float.toString(st6.score),st6);tree.put(st7.name+Integer.toString(st7.height)+Float.toString(st7.score),st7);Collection collection=tree.values();Iterator te=collection.iterator();out.printf("\n\n");out.println("按姓名从小到大排序后的结果是: ");out.println(" 姓名身高成绩体重 ");while(te.hasNext()){Student stu=te.next();out.printf("%-15s %5d %10.2f %6d\n",stu.name,stu.height,stu.score,stu.weight);}}}
自动售货机程序模板 请填写相应代码、编译、运行模板给出的代码,然后完成试验后的练习。MachineSell.javaimport java.util.Scanner;public class MachineSell {public static void main(String args[]){int money;int drinkKind;System.out.printf("投入金额:2或3元(回车确认):");Scanner reader=new Scanner(System.in);money=reader.nextInt();【代码1】//判定投币的面System.out.printf("选择净净矿泉水(1),甜甜矿泉水(2)和美美矿泉水(3)之一:\n");System.out.printf("输入1,2或3:");drinkKind=reader.nextInt();//【代码2】//选择饮料case 1 : System.out.printf("得到净净矿泉水\n");break;case 2 : System.out.printf("得到甜甜矿泉水\n");break;case 3 : System.out.printf("得到美美矿泉水\n");break;default: System.out.printf("选择错误");}}else if(money==3) {System.out.printf("选择爽口可乐(1),清凉雪碧(2),和雪山果汁(3)之一:\n");System.out.printf("输入1,2或3:");drinkKind=reader.nextInt();switch(drinkKind) {case 1 : System.out.printf("得到爽口可乐\n");break;case 2 : System.out.printf("得到清凉雪碧\n");break;case 3 : System.out.printf("得到雪山果汁\n");break;default: System.out.printf("选择错误");}}else {System.out.printf("输入的钱币不符合要求");}}}
猜数字游戏程序模板 请按模板要求,将【代码】替换为Java程序代码。GuessNumber.javaimport java.util.Scanner;import java.util.Random;public class GuessNumber {public static void main (String args[]) {Scanner reader = new Scanner(System.in);Random random = new Random();System.out.println("给你一个1至100之间的整数,请猜测这个数");int realNumber = random.nextInt(100)+1; //random.nextInt(100)是[0,100)中的随机整数int yourGuess = 0;System.out.print("输入您的猜测:");yourGuess = reader.nextInt();while(【代码1】) //循环条件{if(【代码2】) //猜大了的条件代码{System.out.print("猜大了,再输入你的猜测:");yourGuess = reader.nextInt();}else if(【代码3】) //猜小了的条件代码{System.out.print("猜小了,再输入你的猜测:");yourGuess = reader.nextInt();}}System.out.println("猜对了!");}}
程序模板 请按模板要求,将【代码】替换为Java程序代码。Tank.javapublic class Tank {【代码1】//声明double型变量speed,刻画速度【代码2】//声明int型变量bulletAmount,刻画炮弹数量void speedUp(int s) {【代码3】 //将s+speed赋值给speed}void speedDown(int d) {if(speed-d>=0)【代码4】 //将speed-d赋值给speedelsespeed = 0;}void setBulletAmount(int m) {bulletAmount = m;}int getBulletAmount() {return bulletAmount;}double getSpeed() {return speed;}void fire() {if(bulletAmount>=1){【代码5】 //将bulletAmount-1赋值给bulletAmountSystem.out.println("打出一发炮弹");}else {System.out.println("没有炮弹了,无法开火");}}}Fight.javapublic class Fight {public static void main(String args[]) {Tank tank1,tank2;tank1 = new Tank();tank2 = new Tank();tank1.setBulletAmount(10);tank2.setBulletAmount(10);System.out.println("tank1的炮弹数量:"+tank1.getBulletAmount());System.out.println("tank2的炮弹数量:"+tank2.getBulletAmount());tank1.speedUp(80);tank2.speedUp(90);System.out.println("tank1目前的速度:"+tank1.getSpeed());System.out.println("tank2目前的速度:"+tank2.getSpeed());tank1.speedDown(15);tank2.speedDown(30);System.out.println("tank1目前的速度:"+tank1.getSpeed());System.out.println("tank2目前的速度:"+tank2.getSpeed());System.out.println("tank1开火:");tank1.fire();System.out.println("tank2开火:");tank2.fire();tank2.fire();System.out.println("tank1的炮弹数量:"+tank1.getBulletAmount());System.out.println("tank2的炮弹数量:"+tank2.getBulletAmount());}}