在java中分解数字字符串

时间:2014-11-13 01:03:06

标签: java string

我必须打破一个由6个数字(MMCCFU)组成的字符串,其中MM表示月份,CC表示1-15的公寓楼号码,F表示楼层号码(1-3),以及U是一个单位数(1-4)。数字之间没有空格,因此我很难弄清楚如何以这种方式分解字符串。

我需要做的是根据输入的数字创建一个付款簿。公寓的基线费用为1210美元/月。居住在建筑物1-5加上240美元,11-15岁生活在零下240美元。住在一楼的是20美元,三楼的价格是20美元。此外,偶数编号单位的粉丝增加了10美元。这包括每月费用,在1月和7月期间折扣50%。

最终目标是能够创建一个包含所列月份的付款簿,建筑物编号,楼层编号,单元编号以及每个标记的最终价格,并单独列出。

1 个答案:

答案 0 :(得分:1)

以下是该怎么做:

  1. 拒绝任何length超过6的字符串。
  2. 拒绝前两个字符不是月份的任何字符串s
  3. 拒绝任何其下两个字符不是建筑编号的字符串s
  4. 拒绝下一个字符不是楼层号的任何字符串s
  5. 拒绝下一个字符不是单元号的任何字符串s
  6. 创建一个类Book实例化一个实例,然后计算总付款。类似的东西:

    class Book{
        int month;
        int building_number;
        int floor_number;
        int unit_number;
        Book(String line)
        {
           // parse the line 
        }
    
        double computeTotalCost(){
            // retyrb the cost 
        }
    
        boolean validEntry(int month,int building_number,int floor_number, int unit_number)
        {
            // check if the month is between 1 and 12 
            // check if the building_number is between 1 and 15
            // check if the floor_number is between 1 and 3
            // check if the unit_number is between 1 and 5
            // If anything became incorrect : return false otherwise return true;
    
        }
    }