如何将数字串到两个特定的字符串

时间:2014-02-16 21:32:42

标签: java

例如,我有一个名字和姓氏的字符串,然后是该人的统计数据。我如何为另一个名称(字符串)选择不同的统计数据并将它们连接到特定的字符串?

public class project3 
{
    public static void main(String[] args)
    {
        int earnedRuns = 52;
        int inningsPitched = 182;
        double ERA = (earnedRuns * 9.0) / (inningsPitched);
        String FirstName = "Anibal";
        String LastName = "Sanchez";

        System.out.println("Pitcher's first name: " + FirstName);
        System.out.println("Pitcher's last name: " + LastName); 
        System.out.println("Number of earned runs: " + earnedRuns);
        System.out.println("Number of innings pitched: " + inningsPitched);
        System.out.println(FirstName + " " + LastName + " has an ERA of " + ERA);
    }
}

2 个答案:

答案 0 :(得分:0)

您需要有一些课程,将您想要跟踪的所有统计数据分组,例如

public class BaseballPlayer {
  private String firstName;
  private String lastName;
  private int earnedRuns;
  .
  .
  .
}

然后你需要某种地图,将名字或东西映射到那个类。所以你可以做Map<String, BaseballPlayer> playersMap = new HashMap<String, BaseballPlayer>()并添加你的玩家将他们的全名映射到实际的BaseballPlayer实例。

答案 1 :(得分:0)

尝试查看Object-Oriented Programming。谷歌,你可以找到更多关于此的信息。您可以将数据作为对象,类的IE实例进行管理。请参阅下面的示例。

public class Pitcher {

    private String name;
    //Other data about this pitcher. These are called fields, keep them private.

    //This is a getter, use this to access data from outside the class.
    public String getName() {
        return name;
    }

    //This is a constructor, it will be called when you first create the 
    //class using the 'new' keyword.
    public Pitcher(String name) {
        this.name = name;
    }

}

public class Project3 {
    public static void main(String[] args) {
        List<Pitcher> pitchers = new ArrayList();
        pitchers.add(new Pitcher("John Doe"));
    }
}