加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java编程思想第四版第十三章字符串 习题

发布时间:2021-05-21 06:09:33 所属栏目:大数据 来源: https://www.jb51.cc
导读:fas 第二题 package net.mindview.strings;import java.util.ArrayList;import java.util.List; /* * * 无限循环 */ public class InfiniteRecursion { public InfiniteRecursion(){ } @Override String toString() { return " InfiniteRecursion address "
  1. fas
  2. 第二题
    package net.mindview.strings;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 无限循环
     */
    public class InfiniteRecursion {
        public InfiniteRecursion(){
            
        }
        @Override
         String toString() {
            return " InfiniteRecursion address" + super.toString() + n";
        }
        static void main(String[] args) {
            List<InfiniteRecursion> list = new ArrayList<InfiniteRecursion>();
            for(int i=0; i<5; i++){
                list.add(new InfiniteRecursion());
            }
            System.out.println(list);
        }
    }

    ?

  3. fa
  4. 第四题
    package net.mindview.strings;
    
    import java.util.Formatter;
    
     Receipt {
        private double total = 0;
        private Formatter f = new Formatter(System.);
        public final int ITEM_WIDTH = 15int QTY_WIDTH = 5int PRICE_WIDTH = 10int PRICISION_FLOAT_WIDHT = 2int PRICISION_String_WIDHT = public final String TITLE_FORMAT = %-" + ITEM_WIDTH + s %" + QTY_WIDTH
                + " + PRICE_WIDTH + snpublic final String PRICE_FORMAT = ."
                + PRICISION_String_WIDHT + " + QTY_WIDTH + d % PRICE_WIDTH
                + " + PRICISION_FLOAT_WIDHT + fn; 
        public final String TOTAL_FORMAT = ; 
        
        //打印标题
         printTitle(){
            
             * 含义: 格式化字符串串以%开头
             * -: 表示左对齐
             * 15: 15表示宽度
             * s:表示数据的类型是String
             * .2f:表示浮点数保留的小数位数
             * .15s:表示字符串的长度最多15个字符 
             */
            f.format(TITLE_FORMAT,Item",QtyPrice);
            f.format(TITLE_FORMAT,1)">------------);
        }
        
        正文的内容
        void print(String name,int qty,1)">double price){
            f.format(PRICE_FORMAT,name,qty,price);
            total += price;
        }
        
        总价
         printTotal(){
            f.format(TOTAL_FORMAT,1)">Tax"",total*0.06);
            f.format(TOTAL_FORMAT,1)">Total1.06 main(String[] args) {
            Receipt receipt =  Receipt();
            receipt.printTitle();
            receipt.print(Jack`s Magic Beans4,1)">4.25);
            receipt.print(Princess Peas3,1)">5.1Three Bears Porridge1,1)">14.29);
            receipt.printTotal();
        }
    
    }

    运行结果:

    Item              Qty      Price
    ----              ---      -----
    Jack`s Magic Be     4       
    Princess Peas       3       5.10
    Three Bears Por     1      
    Tax                         1.42
                               -----
    Total                      25.06

    ?

  5. 第五题
    );
            receipt.printTotal();
        }
    
    }

    运行结果:

    25.06

    ?

  6. afd
  7. 第七题:
    package net.mindview.strings.test7;
    
     Test7 {
    
         main(String[] args) {
            两种写法都可以
            String regex = ^[A-Z].*.$;
            String regex1 = p{Upper}.*.$;
            String str = D.;
            String str1 = Dfasdfasfasfdasfdasfasfasdf.;
            String str2 = Dfasdfasfasfdasfdasfasfasdf.E;
            System..println(str.matches(regex));
            System..println(str1.matches(regex));
            System..println(str2.matches(regex));
            System..println(str.matches(regex1));
            System..println(str1.matches(regex1));
            System..println(str2.matches(regex1));
        }
    }

    ?

    运行结果:
    true
    false
    false

    ?

  8. 第八题
    package net.mindview.strings;
    
    import java.util.Arrays;
    
     Splitting {
    
        static String knights = Then,when you have found the shrubbery,you must cut down the mightiest tree in the forest... with... a herring!;
    
         split(String regex){
            System..println(Arrays.toString(knights.split(regex)));
        }
        表示的时按照空格分割字符串
            运行结果:[Then,when,you,have,found,the,shrubbery,must,cut,down,mightiest,tree,in,forest...,with...,a,herring!]
            split(" );
            
            表示按照非单次字符分割字符串--这里的非单次字符是空格和,W+);
            这个表示:费单次字符之前带n的地方进行分割字符串 这里的分割符是n空格和n,1)">运行结果:[The,whe,you have found the shrubbery,you must cut dow,the mightiest tree i,the forest... with... a herring!]
            split(nW+);
        }
    
    }

    ?

    package net.mindview.strings.test8;
    
    import net.mindview.strings.Splitting;
    
     Test8 {
        
         main(String[] args) {
            String regex = the|you;
            Splitting.split(regex);
        }
    }

    ?

  9. 第九题
    package net.mindview.strings.test9;
    
    import net.mindview.strings.Splitting;
    
     Test9 {
        A|E|I|O|U|a|e|i|o|u;
            通过嵌入式标志表达式  (?i) 也可以启用不区分大小写的匹配。
            String regex1 = (?i)a|e|i|o|u[abc] 表示a或b或c
            String regex2 = (?i)[aeIoU]out.println(Splitting.knights.replaceAll(regex,1)">_));
            System.out.println(Splitting.knights.replaceAll(regex1,1)">out.println(Splitting.knights.replaceAll(regex2,1)">));
        }
    }

    ?

  10. 第十题
    package net.mindview.strings.test10;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
     Test10 {
         main(String[] args) {
            String str = Java now has regular expressions;
            
            for(String arg: args){
                Pattern p = Pattern.compile(arg);
                Matcher m = p.matcher(str);
                while(m.find()){
                    System.out.println(Match "" + m.group() +" at positions " + m.start() + -" + (m.end()-1) );
                }
                System.);
            }
            
        }
    }

    输入参数:

    ^Java
    Breg.*
    n.ws+h(a|i)s
    s?
    s*
    s+
    s{4}
    s{}.
    s{0,1)">3}

    输出结果

    Match Java" at positions 0-3
    
    
    
    
    Match now has5-11
    
    
    Match "" at positions 0--
    Match 1-2-3-4-6-7-68-79-810-9s11-12-13-1214-1315-1416-17-1618-1719-1820-1921-2022-2123-2224-2325-2426-2627-2728-29-2830-2931-3132-sss 31

    ?

  11. 第十一题
    package net.mindview.strings.test11;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
     Test11 {
         main(String[] args) {
            Pattern p = Pattern.compile((?i)((^[aeIoU])|(s+[aeIoU]))w+?[aeIoU]b);
            Matcher m = p.matcher(Arline ate eight apples and " +
    one orange while Anita hadn't any(m.find()){
                System." + m.group() +
                        
                        (m.end() - ));
            }
        }
    }    

    运行结果:

    Match Arline ate one30 orange37 Anita44-49

    ?

  12. sfa
  13. f
  14. a
  15. fda
  16. sdf
  17. af
  18. a
  19. fd
  20. af
  21. as
  22. fd
  23. asfd
  24. af
  25. da
  26. f
  27. df
  28. as
  29. fa
  30. sf
  31. asf
  32. a
  33. sf
  34. af
  35. asf

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读