永发信息网

java程序出问题,arraylist里添加了5个对象,最后却出现了null!求指错

答案:1  悬赏:20  手机版
解决时间 2021-01-28 17:26
  • 提问者网友:饥饿走向夜
  • 2021-01-28 08:33
public class Monster{
String name;
public Monster(){};
public Monster(String name){
this.name=name;
}
public int attack(){
int dama = (int)((Math.random() * 4+1));
System.out.println(name + ", of "+this.getClass()+", attacks generically:"+dama+"points damage caused.");
return dama;
}//generic monster attack() method should return a random integer value between 1 and 5
public void move(int direction) {
switch(direction) {
case 1:
System.out.println(this.name + " is moving 1 step NORTH.");
break;
case 2:
System.out.println(this.name + " is moving 1 step EAST.");
break;
case 3:
System.out.println(this.name + " is moving 1 step SOUTH.");
break;
default:
System.out.println(this.name + " is moving 1 step WEST.");
break;
}
}
}

public class Dragon extends Monster{
String name;
public Dragon(String name){
this.name=name;}
public int attack(){
int dama = 0;
double a = Math.random();
if(a>0.3 && a<1){
super.attack();
}
else{
dama = (int)((Math.random() * 49+1));
System.out.println(this.name + ", of "+this.getClass()+", attacks breathing fire:"+dama+"points damage caused.");
}
return dama;
}//attacks by breathing fire 30% of the time and generically the rest of the time. When it attacks by breathing fire, it causes between 1 and 50 points of damage;
}
public class Troll extends Monster{
String name;
public Troll(String n){
if(n.equals("Saul") || n.equals("Salomon")){
System.out.println("Error");
this.name = "Detritus";}
else{
this.name = n;}//cannot be named Saul or Salomon. If the user of your program attempts to misname a troll, your program should print an error message and name the troll Detritus
}
}

public class MonsterMash {
public static void main(String[] args) {
ArrayList monsters = new ArrayList();
monsters.add(new Monster("Tom"));
monsters.add(new Monster("George"));
monsters.add(new Dragon("Smaug"));
monsters.add(new Dragon("Jabosh"));
monsters.add(new Troll("Salomon"));
monsters.add(new Troll("Bender"));
int damageDone = 0;
while (damageDone < 100) {
for (Monster m : monsters) {
m.move((int)(Math.random()*4) + 1);
damageDone += m.attack();
}
}
}
}
最佳答案
  • 五星知识达人网友:何以畏孤独
  • 2021-01-28 09:24
问题出在了this.name,
给每个类都加一个方法:
public String getName(){
 return this.name;
 }


然后把需要取得name的地方,都换成this.getName()
import java.util.*;
class Monster{
String name;
public Monster(){};
public Monster(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public int attack(){
int dama = (int)((Math.random() * 4+1));
System.out.println(this.getName() + ", of "+this.getClass()+", attacks generically:"+dama+"points damage caused.");
return dama;
}//generic monster attack() method should return a random integer value between 1 and 5
public void move(int direction) {
switch(direction) {
case 1:
System.out.println(this.getName() + " is moving 1 step NORTH.");
break;
case 2:
System.out.println(this.getName() + " is moving 1 step EAST.");
break;
case 3:
System.out.println(this.getName() + " is moving 1 step SOUTH.");
break;
default:
System.out.println(this.getName() + " is moving 1 step WEST.");
break;
}
}
}
class Dragon extends Monster{
String name;
public Dragon(String name){
this.name=name;}
public String getName(){
return this.name;
}
public int attack(){
int dama = 0;
double a = Math.random();
if(a>0.3 && a<1){
super.attack();
}
else{
dama = (int)((Math.random() * 49+1));
System.out.println(this.getName() + ", of "+this.getClass()+", attacks breathing fire:"+dama+"points damage caused.");
}
return dama;
}//attacks by breathing fire 30% of the time and generically the rest of the time. When it attacks by breathing fire, it causes between 1 and 50 points of damage;
}
class Troll extends Monster{
String name;
public Troll(String n){
if(n.equals("Saul") || n.equals("Salomon")){
System.out.println("Error");
this.name = "Detritus";}
else{
this.name = n;}//cannot be named Saul or Salomon. If the user of your program attempts to misname a troll, your program should print an error message and name the troll Detritus
}
public String getName(){
return this.name;
}
}
public class MonsterMash {
public static void main(String[] args) {
ArrayList monsters = new ArrayList();
monsters.add(new Monster("Tom"));
monsters.add(new Monster("George"));
monsters.add(new Dragon("Smaug"));
monsters.add(new Dragon("Jabosh"));
monsters.add(new Troll("Salomon"));
monsters.add(new Troll("Bender"));
int damageDone = 0;
while (damageDone < 100) {
for (Monster m : monsters) {
m.move((int)(Math.random()*4) + 1);
damageDone += m.attack();
}
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯