0

我正在尝试创建一个程序,通过让三个“讲故事的人”对原始故事进行更改来修改我的初始故事。但是,当我尝试运行 TellStory 时,程序只为最终重述的故事打印“nullnullnull”。我做错什么了?非常感谢您的宝贵时间 :)

public class TellStory{
public String story;
public static void main(String[]args){
    String story="The three little pigs went shopping for lettuce. When they arrived at          the grocery store, they found they had forgotten their money. They asked the manager if they could have some credit, but he refused. The three little pigs saw their friend, Mr. Turtle, and he agreed to give them a loan. They went home happy and had a wonderful meal!";
    System.out.println("Welcome to my Story");
    System.out.println("");
    System.out.println("Initial Story:");
    System.out.println(story);
    System.out.println("");

    Storyteller1 Frank=new Storyteller1();
    Storyteller2 John=new Storyteller2();
    Storyteller3 Jake=new Storyteller3();

    String storyRetoldByFrank=Frank.getStory1();
    String storyRetoldByJohn=John.getStory2();
    String storyRetoldByJake=Jake.getStory3();

    String FinalRetoldStory=storyRetoldByFrank+storyRetoldByJohn+storyRetoldByJake;

    System.out.println("Final Retold Story:");
    System.out.println(FinalRetoldStory);
    }
}

public class Storyteller1 extends TellStory{
public String story1;
public String retellStory(String story1){
    int startIndex1=story.indexOf("three little pigs went");
    String subStr1=story.substring(startIndex1,startIndex1+17);
    String subStr2=story.replaceAll(subStr1,"four chickens");

    int startIndex2=story.indexOf("lettuce. When");
    String subStr3=story.substring(startIndex2,startIndex2+8);
    String subStr4=story.replaceAll(subStr3,"raisins");

    int startIndex3=story.indexOf("grocery store, they");
    String subStr5=story.substring(startIndex3,startIndex3+14);
    String subStr6=story.replaceAll(subStr5,"mall");

    int startIndex4=story.indexOf("money. They");
    String subStr7=story.substring(startIndex4,startIndex4+6);
    String subStr8=story.replaceAll(subStr7,"friend");

    story1=subStr2+" went shopping for "+subStr4+". "+"When they arrived at the    "+subStr6+", they found they had forgotten their "+subStr8+".";
    return story1;
    }
     public String getStory1(){
     return story1;
     }
}

public class Storyteller2 extends TellStory{
public String story2;
public String retellStory(String story2){
    int startIndex5=story.indexOf("manager");
    String subStr9=story.substring(startIndex5,startIndex5+8);
    String subStr10=story.replaceAll(subStr9,"security guard");

    int startIndex6=story.indexOf("some credit");
    String subStr11=story.substring(startIndex6,startIndex6+12);
    String subStr12=story.replaceAll(subStr11,"a phone");

    story2="They asked the "+subStr11+" if they could have "+subStr11+", but he refused.";
    return story2;
    }
     public String getStory2(){
     return story2;
     }
}

public class Storyteller3 extends TellStory{
public String story3;
public String retellStory(String story3){
    int startIndex7=story.indexOf("friend");
    String subStr13=story.substring(startIndex7,startIndex7+7);
    String subStr14=story.replaceAll(subStr13,"nemesis");

    int startIndex8=story.indexOf("Mr. Turtle");
    String subStr15=story.substring(startIndex8,startIndex8+11);
    String subStr16=story.replaceAll(subStr15,"Fearsome Fox");

    int startIndex9=story.indexOf("loan");
    String subStr17=story.substring(startIndex9,startIndex9+5);
    String subStr18=story.replaceAll(subStr17,"ride");

    int startIndex10=story.indexOf("happy and");
    String subStr19=story.substring(startIndex10,startIndex10+10);
    String subStr20=story.replaceAll(subStr19,"with Fearsome Fox - he");

    story3="The three little pigs saw their "+subStr14+","+subStr16+" and he agreed to     give them a "+subStr18+". They went home "+subStr20+"had a wonderful meal!";
    return story3;
    }
     public String getStory3(){
     return story3;
     }
}
4

2 回答 2

3
  1. 没有人调用该retellStory方法,因此没有创建任何内容......
  2. 这些值story1story2并且story3从未被分配任何值......
  3. story被 , 访问Storyteller1的实例字段永远不会被赋值,并且会被赋值Storyteller2,从而导致Storyteller3nullNullPointerException
于 2013-10-14T00:06:12.227 回答
1

retellStory方法永远不会被StoryTeller.

将方法添加retellStory到每个的构造函数StoryTeller 或在初始化每个后添加对方法的调用StoryTeller

于 2013-10-14T00:11:19.050 回答