我正在尝试制作简单的口袋妖怪游戏如何将toString方法从Trainer.java到调用Arena.toString

【问题描述】:

我正在尝试制作一个简单的口袋妖怪游戏。

如何从 Trainer.java 调用 toString 方法到 Arena.java?我尝试使用 this.trainer1.toString() 但它不起作用。 Arena.java 也没有 main 方法。

这是文件

Trainer.java

import java.util.Random;
public class Trainer {
    private String nameTrainer;
    private Taschenmonster[] inventar = new Taschenmonster[6];
    // getter and setter Methods for attributes
    /*
     * getter and setter for nameTrainer
     */
    public String getNameTrainer() {
        return this.nameTrainer;
    }
    public void setNameTrainer(String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }
    /*
     * getter and setter for inventar
     */
    // public Taschenmonster getInventar() {
    //     return this.inventar;
    // }
    // public void setName(String nameTrainer) {
    //     this.nameTrainer = nameTrainer;
    // }
    // constructor of class Trainer
    Trainer (String nameTrainer) {
        this.nameTrainer = nameTrainer;
    }
    /*
     * Adds Taschenmonster to Inventar of Trainer.
     */
    public void receive(int pos, Taschenmonster monsterObject) {
       if (pos < 6) {
           inventar[pos] = monsterObject;
       } 
    }
    /*
     * give out a randomly selected Taschenmonster for battle
     */
    public Taschenmonster send() {
        int random = new Random().nextInt(inventar.length);
        return inventar[random];
    }
    /*
     * Overriding the string method 
     */
    @Override
    public String toString() {
        return "Taschenmonster von " + nameTrainer + "n" + inventar[0].getNameMonster() + " " + inventar[1].getNameMonster() + " " + inventar[2].getNameMonster() + " " + inventar[3].getNameMonster() + " " + inventar[4].getNameMonster() + " " + inventar[5].getNameMonster();    
    }
    
}

Arena.java

public class Arena {
    private Trainer trainer1;
    private Trainer trainer2;
    // constructor of class Arena
    Arena (Trainer trainer1, Trainer trainer2) {
        this.trainer1 = trainer1;
        this.trainer2 = trainer2;
    }
    // giving out the profile of the trainers
    // this.trainer1.toString();
    // this.trainer2.toString();
}

Main.java

public class Main {
    public static void main(String args[]) {
        // making objects from class Taschenmonster
        Taschenmonster t1 = new Taschenmonster("Pikachu", 5, 7);
        Taschenmonster t2 = new Taschenmonster("Eevee", 5, 6);
        Taschenmonster t3 = new Taschenmonster("Charmander", 5, 4);
        Taschenmonster t4 = new Taschenmonster("Bulbasaur", 5, 5);
        Taschenmonster t5 = new Taschenmonster("Squirtle", 5, 2);
        Taschenmonster t6 = new Taschenmonster("Mewoth", 5, 3);
        // instantiating trainers from class Trainer
        Trainer trainer1 = new Trainer("Paul");
        Trainer trainer2 = new Trainer("Chris");
        // adding Taschenmonster to both trainers
        trainer1.receive(0, t1);
        trainer1.receive(1, t2);
        trainer1.receive(2, t3);
        trainer1.receive(3, t4);
        trainer1.receive(4, t5);
        trainer1.receive(5, t6);
        trainer2.receive(0, t1);
        trainer2.receive(1, t2);
        trainer2.receive(2, t3);
        trainer2.receive(3, t4);
        trainer2.receive(4, t5);
        trainer2.receive(5, t6);
        // giving out profiles of Players
        System.out.println(trainer1);
        System.out.println(trainer2);
    }
}

Taschenmonster.java

import java.util.*;
public class Taschenmonster {
    // class attributes
    private String nameMonster;
    private int lives, damageValue;
    // getter and setter Methods for attributes
    /*
     * getter and setter for nameMonster
     */
    public String getNameMonster() {
        return this.nameMonster;
    }
    public void setNameMonster(String nameMonster) {
        this.nameMonster = nameMonster;
    }
    /*
     * getter and setter for lives
     */
    public int getLives() {
        return this.lives;
    }
    public void setLives(int lives) {
        this.lives = lives;
    }
    /*
     * getter and setter for damageValue
     */
    public int getDamageValue() {
        return this.damageValue;
    }
    public void setDamageValue(int damageValue) {
        this.damageValue = damageValue;
    }
    // constructor of class Taschenmonster
    Taschenmonster (String nameMonster, int lives, int damageValue) {
        this.nameMonster = nameMonster;
        this.lives = lives;
        this.damageValue = damageValue;
    }
    /*
     * how much lives is remaining with Taschenmonster
     */
    public void receiveDamage() {
        this.lives--;
        if (this.lives == 0) {
            System.out.println("Caution! No lives left.");
        }
    }
    /*
     * gives out estimated attackValue of Taschenmonster in form of double. 
     */
    public int attackValue() {
        Random random = new Random();
        return this.damageValue *= random.nextInt() % 2;
    }
    /*
     * Overriding toString() Method and values of object 
     */
    @Override
    public String toString() {
        return nameMonster + " " + lives + " " + damageValue;
    }
}

【讨论】:

© 版权声明
THE END
喜欢就支持一下吧
点赞167 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片