Java – returns the class name instead of the class name, and the memory address

I want the two teams to play against each other When I say team1 I call it play (team2);

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!");
    }

}

Solution

You need to make the following two changes in the team course

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!");
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>