Java 从方法返回 null 值

huangapple 未分类评论67阅读模式
英文:

Java returning null value from method

问题

ELF 1 [ STATUS: 活着的精灵 ] [ CLAN: 森林 | BEST FRIEND: null ] [ STR: 5 | DEX: 7 | ARM: 1 | MOX: 16 ] [ COINS: 77 | HEALTH: 80 ]

ELF 2 [ STATUS: 活着的精灵 ] [ CLAN: 城市 | BEST FRIEND: HOBBIT 1 ] [ STR: 14 | DEX: 20 | ARM: 18 | MOX: 12 ] [ COINS: 1000 | HEALTH: 80 ]

主类部分:

  1. // HOBBIT 1 和 2
  2. System.out.println("\n让我们创建两个新的霍比特人!");
  3. System.out.println("正在创建两个新的霍比特人...");
  4. System.out.println("\n霍比特人 1 的名字:");
  5. String hobbitName = sc.nextLine();
  6. Hobbit hobbit1 = new Hobbit(hobbitName);
  7. System.out.println("霍比特人 2 的名字:");
  8. String hobbitName2 = sc.nextLine();
  9. Hobbit hobbit2 = new Hobbit(hobbitName2, 8, 12, 2, 14, 10, 30);
  10. System.out.println("\n" + hobbit1);
  11. System.out.println(hobbit2);
  12. // ELF 1 和 2
  13. System.out.println("\n让我们创建两个新的精灵!");
  14. System.out.println("正在创建两个新的精灵...");
  15. System.out.println("\n精灵 1 的名字:");
  16. String elfName1 = sc.nextLine();
  17. Elf elf1 = new Elf(elfName1);
  18. System.out.println("精灵 2 的名字:");
  19. String elfName2 = sc.nextLine();
  20. Elf elf2 = new Elf(elfName2, 14, 20, 18, 12, 1000, 80, "森林", hobbit1);
  21. System.out.println("\n" + elf1);
  22. System.out.println(elf2);

精灵类代码部分:

  1. import java.util.Random;
  2. public class Elf extends Humanoid {
  3. private final String clan;
  4. private String bestFriend;
  5. public Elf(String name) {
  6. super(name);
  7. // 随机设置氏族
  8. Random random = new Random();
  9. if(random.nextBoolean()){
  10. clan = "森林";
  11. } else {
  12. clan = "城市";
  13. }
  14. }
  15. public Elf(String name, String clan, Hobbit bestFriend) {
  16. super(name);
  17. this.clan = clan;
  18. this.bestFriend = bestFriend.getName();
  19. }
  20. public Elf(String name, int strength, int dexterity, int armour, int moxie, int coins, int healthRating, String clan, Hobbit bestFriend) {
  21. super(name, strength, dexterity, armour, moxie, coins, healthRating);
  22. this.clan = clan;
  23. this.bestFriend = bestFriend.getName();
  24. }
  25. public String getBestFriend() {
  26. return this.bestFriend;
  27. }
  28. public void setBestFriend(Hobbit bestFriend) {
  29. this.bestFriend = bestFriend.getName();
  30. }
  31. public String getClan() {
  32. return clan;
  33. }
  34. // 仅适用于精灵 2 的行
  35. @Override
  36. public String toString() {
  37. return getName() +
  38. " [ STATUS: " + super.isAlive() +
  39. " 精灵 ] [ CLAN: " + clan +
  40. " | BEST FRIEND: " + getBestFriend() +
  41. " ]" + super.toString();
  42. }
  43. }
英文:

I keep having a object come up as null for Elf 1 which is supposed to show BEST FRIEND: HOBBIT 2 but it only shows null. I tried to look for solutions but nothing helped me. Would anyone please help me figure out a solution? Thank you.

example output:

ELF 1 [ STATUS: Alive Elf ] [ CLAN: Forest | BEST FRIEND: null ] [ STR: 5 | DEX: 7 | ARM: 1 | MOX: 16 ] [ COINS: 77 | HEALTH: 80 ]

ELF 2 [ STATUS: Alive Elf ] [ CLAN: City | BEST FRIEND: HOBBIT 1 ] [ STR: 14 | DEX: 20 | ARM: 18 | MOX: 12 ] [ COINS: 1000 | HEALTH: 80 ]

main class

  1. // HOBBIT 1 AND 2
  2. System.out.println("\nLet's create new two Hobbits!");
  3. System.out.println("Building two new hobbits...");
  4. System.out.println("\nHobbit 1 name: ");
  5. String hobbitName = sc.nextLine();
  6. Hobbit hobbit1 = new Hobbit(hobbitName);
  7. System.out.println("Hobbit 2 name: ");
  8. String hobbitName2 = sc.nextLine();
  9. Hobbit hobbit2 = new Hobbit(hobbitName2, 8, 12, 2, 14, 10, 30);
  10. System.out.println("\n" + hobbit1);
  11. System.out.println(hobbit2);
  12. // ELF 1 AND 2
  13. System.out.println("\nLet's create new two Elves!");
  14. System.out.println("Building two new Elves...");
  15. System.out.println("\nElf 1 name: ");
  16. String elfName1 = sc.nextLine();
  17. Elf elf1 = new Elf(elfName1);
  18. System.out.println("Elf 2 name: ");
  19. String elfName2 = sc.nextLine();
  20. Elf elf2 = new Elf(elfName2, 14, 20, 18, 12, 1000, 80, "Forest", hobbit1);
  21. System.out.println("\n" + elf1);
  22. System.out.println(elf2);

code in elf class

  1. public class Elf extends Humanoid {
  2. private final String clan;
  3. private String bestFriend;
  4. public Elf(String name) {
  5. super(name);
  6. // randomly sets clan
  7. Random random = new Random();
  8. if(random.nextBoolean()){
  9. clan = "Forest";
  10. } else {
  11. clan = "City";
  12. }
  13. }
  14. public Elf(String name, String clan, Hobbit bestFriend) {
  15. super(name);
  16. this.clan = clan;
  17. this.bestFriend = bestFriend.getName();
  18. }
  19. public Elf(String name, int strength, int dexterity, int armour, int moxie, int coins, int healthRating, String clan, Hobbit bestFriend) {
  20. super(name, strength, dexterity, armour, moxie, coins, healthRating);
  21. this.clan = clan;
  22. this.bestFriend = bestFriend.getName();
  23. }
  24. public String getBestFriend() {
  25. return this.bestFriend;
  26. }
  27. public void setBestFriend(Hobbit bestFriend) {
  28. this.bestFriend = bestFriend.getName();
  29. }
  30. public String getClan() {
  31. return clan;
  32. }
  33. // only for elf 2 line
  34. @Override
  35. public String toString() {
  36. return getName() +
  37. " [ STATUS: " + super.isAlive() +
  38. " Elf ] [ CLAN: " + clan +
  39. " | BEST FRIEND: " + getBestFriend() +
  40. " ]" + super.toString();
  41. }
  42. }```
  43. </details>
  44. # 答案1
  45. **得分**: 2
  46. 你正在调用构造函数 `Elf(String)` 来创建 elf1;这个构造函数设置了族群和名字,仅此而已。没有任何东西会设置“最好的朋友”,所以它保持为空。
  47. <details>
  48. <summary>英文:</summary>
  49. You’re calling the constructor `Elf(String)` to create elf1; this constructor sets the clan and the name and that’s all. Nothing ever sets the “best friend” so it remains null.
  50. </details>

huangapple
  • 本文由 发表于 2020年7月27日 10:05:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63107730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定