java – 返回类名而不是返回类名,返回内存地址
发布时间:2020-12-15 05:21:30 所属栏目:Java 来源:网络整理
导读:我想让两支队伍互相比赛.当我说team1.play(team2)时我称之为; ?当我生成一个小于0.5的数字时,team2应该获胜,如果它大于0.5,那么第1队应该获胜.当1队获胜时,它会正确显示为尼克斯,但是当team2获胜时,它会显示内存地址.我如何才能正确地说网队赢了而不是球队@
我想让两支队伍互相比赛.当我说team1.play(team2)时我称之为;
?当我生成一个小于0.5的数字时,team2应该获胜,如果它大于0.5,那么第1队应该获胜.当1队获胜时,它会正确显示为尼克斯,但是当team2获胜时,它会显示内存地址.我如何才能正确地说网队赢了而不是球队@ 78987neu73 public class teams{ public static void main(String [] argv){ team team1 = new team("knicks"); team team2 = new team("nets"); team1.lose(); team2.win(); team2.lose(); team2.printrecord(); team1.play(team2); }//main }//teams class team{ int wins; int losses; String name; public team(String n){ name = n; wins = losses = 0; }//constructor public void lose(){ losses++; }//losses public void win(){ wins++; }//wins public void printrecord(){ System.out.println("W-L: " +wins+"-"+losses); } public void play(team j){ if((Math.random())<0.5){ System.out.println("The "+j+" Have Won!"); }//if else System.out.println("The "+name+" Have Won!"); } } 解决方法
您需要在团队课程中进行以下两项修改
public String getName() { return name; } public void play(team j){ if((Math.random())<0.5){ System.out.println("The "+j.getName()+" Have Won!"); }//if else System.out.println("The "+name+" Have Won!"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |