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

简介: 运行结果: 运行结果: 输入参数: 输出结果


1.fas


2.第二题


package net.mindview.strings;
import java.util.ArrayList;
import java.util.List;
/**
 * 无限循环
 */
public class InfiniteRecursion {
    public InfiniteRecursion(){
    }
    @Override
    public String toString() {
        return " InfiniteRecursion address" + super.toString() + "\n";
    }
    public 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;
public class Receipt {
    private double total = 0;
    private Formatter f = new Formatter(System.out);
    public final int ITEM_WIDTH = 15;
    public final int QTY_WIDTH = 5;
    public final int PRICE_WIDTH = 10;
    public final int PRICISION_FLOAT_WIDHT = 2;
    public final int PRICISION_String_WIDHT = 15;
    public final String TITLE_FORMAT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH
            + "s %" + PRICE_WIDTH + "s\n";
    public final String PRICE_FORMAT = "%-" + ITEM_WIDTH + "."
            + PRICISION_String_WIDHT + "s %" + QTY_WIDTH + "d %" + PRICE_WIDTH
            + "." + PRICISION_FLOAT_WIDHT + "f\n"; 
    public final String TOTAL_FORMAT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH
            + "s %" + PRICE_WIDTH + "." + PRICISION_FLOAT_WIDHT + "f\n"; 
    //打印标题
    public void printTitle(){
        /*
         * 含义: 格式化字符串串以%开头
         * -: 表示左对齐
         * 15: 15表示宽度
         * s:表示数据的类型是String
         * .2f:表示浮点数保留的小数位数
         * .15s:表示字符串的长度最多15个字符 
         */
        f.format(TITLE_FORMAT, "Item", "Qty", "Price");
        f.format(TITLE_FORMAT, "----", "---", "-----");
    }
    //正文的内容
    public void print(String name, int qty, double price){
        f.format(PRICE_FORMAT, name, qty, price);
        total += price;
    }
    //总价
    public void printTotal(){
        f.format(TOTAL_FORMAT, "Tax", "", total*0.06);
        f.format(TITLE_FORMAT, "", "", "-----");
        f.format(TOTAL_FORMAT, "Total", "", total*1.06);
    }
    public static void main(String[] args) {
        Receipt receipt = new Receipt();
        receipt.printTitle();
        receipt.print("Jack`s Magic Beans", 4, 4.25);
        receipt.print("Princess Peas", 3, 5.1);
        receipt.print("Three Bears Porridge", 1, 14.29);
        receipt.printTotal();
    }
}


运行结果:


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


5.第五题


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


运行结果:


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


6.afd


7.第七题:


package net.mindview.strings.test7;
public class Test7 {
    public static void main(String[] args) {
        //两种写法都可以
        String regex = "^[A-Z].*\\.$";
        String regex1 = "\\p{Upper}.*\\.$";
        String str = "D.";
        String str1 = "Dfasdfasfasfdasfdasfasfasdf.";
        String str2 = "Dfasdfasfasfdasfdasfasfasdf.E";
        System.out.println(str.matches(regex));
        System.out.println(str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str.matches(regex1));
        System.out.println(str1.matches(regex1));
        System.out.println(str2.matches(regex1));
    }
}


运行结果:


true
true
false
true
true
false


8.第八题


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


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


9.第九题


package net.mindview.strings.test9;
import net.mindview.strings.Splitting;
public class Test9 {
    public static void main(String[] args) {
        String regex = "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]";
        System.out.println(Splitting.knights.replaceAll(regex, "_"));
        System.out.println(Splitting.knights.replaceAll(regex1, "_"));
        System.out.println(Splitting.knights.replaceAll(regex2, "_"));
    }
}


10.第十题


package net.mindview.strings.test10;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test10 {
    public static void 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.out.println("\n");
        }
    }
}


输入参数:


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


输出结果


Match "Java" at positions 0-3
Match "now has" at positions 5-11
Match "" at positions 0--1
Match "" at positions 1-0
Match "" at positions 2-1
Match "" at positions 3-2
Match "" at positions 4-3
Match "" at positions 5-4
Match "" at positions 6-5
Match "" at positions 7-6
Match "" at positions 8-7
Match "" at positions 9-8
Match "" at positions 10-9
Match "s" at positions 11-11
Match "" at positions 12-11
Match "" at positions 13-12
Match "" at positions 14-13
Match "" at positions 15-14
Match "" at positions 16-15
Match "" at positions 17-16
Match "" at positions 18-17
Match "" at positions 19-18
Match "" at positions 20-19
Match "" at positions 21-20
Match "" at positions 22-21
Match "" at positions 23-22
Match "" at positions 24-23
Match "" at positions 25-24
Match "s" at positions 26-26
Match "s" at positions 27-27
Match "" at positions 28-27
Match "" at positions 29-28
Match "" at positions 30-29
Match "s" at positions 31-31
Match "" at positions 32-31
Match "s" at positions 11-11
Match "ss" at positions 26-27
Match "s" at positions 31-31
Match "s " at positions 11-12
Match "ss" at positions 26-27
Match "" at positions 0--1
Match "" at positions 1-0
Match "" at positions 2-1
Match "" at positions 3-2
Match "" at positions 4-3
Match "" at positions 5-4
Match "" at positions 6-5
Match "" at positions 7-6
Match "" at positions 8-7
Match "" at positions 9-8
Match "" at positions 10-9
Match "s" at positions 11-11
Match "" at positions 12-11
Match "" at positions 13-12
Match "" at positions 14-13
Match "" at positions 15-14
Match "" at positions 16-15
Match "" at positions 17-16
Match "" at positions 18-17
Match "" at positions 19-18
Match "" at positions 20-19
Match "" at positions 21-20
Match "" at positions 22-21
Match "" at positions 23-22
Match "" at positions 24-23
Match "" at positions 25-24
Match "ss" at positions 26-27
Match "" at positions 28-27
Match "" at positions 29-28
Match "" at positions 30-29
Match "s" at positions 31-31
Match "" at positions 32-31


11.第十一题


package net.mindview.strings.test11;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test11 {
    public static void 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");
        while(m.find()){
            System.out.println("Match \"" + m.group() +
                    "\" at positions " + m.start() + "-" +
                    (m.end() - 1));
        }
    }
}    


运行结果:


Match "Arline" at positions 0-5
Match " ate" at positions 6-9
Match " one" at positions 27-30
Match " orange" at positions 31-37
Match " Anita" at positions 44-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


36.


相关文章
|
1月前
|
Java 开发者
Java多线程编程中的常见误区与最佳实践####
本文深入剖析了Java多线程编程中开发者常遇到的几个典型误区,如对`start()`与`run()`方法的混淆使用、忽视线程安全问题、错误处理未同步的共享变量等,并针对这些问题提出了具体的解决方案和最佳实践。通过实例代码对比,直观展示了正确与错误的实现方式,旨在帮助读者构建更加健壮、高效的多线程应用程序。 ####
|
23天前
|
Java 程序员
Java编程中的异常处理:从基础到高级
在Java的世界中,异常处理是代码健壮性的守护神。本文将带你从异常的基本概念出发,逐步深入到高级用法,探索如何优雅地处理程序中的错误和异常情况。通过实际案例,我们将一起学习如何编写更可靠、更易于维护的Java代码。准备好了吗?让我们一起踏上这段旅程,解锁Java异常处理的秘密!
|
28天前
|
SQL Java 索引
java小工具util系列2:字符串工具
java小工具util系列2:字符串工具
142 83
|
3天前
|
存储 缓存 Java
Java 并发编程——volatile 关键字解析
本文介绍了Java线程中的`volatile`关键字及其与`synchronized`锁的区别。`volatile`保证了变量的可见性和一定的有序性,但不能保证原子性。它通过内存屏障实现,避免指令重排序,确保线程间数据一致。相比`synchronized`,`volatile`性能更优,适用于简单状态标记和某些特定场景,如单例模式中的双重检查锁定。文中还解释了Java内存模型的基本概念,包括主内存、工作内存及并发编程中的原子性、可见性和有序性。
Java 并发编程——volatile 关键字解析
|
7天前
|
算法 Java 调度
java并发编程中Monitor里的waitSet和EntryList都是做什么的
在Java并发编程中,Monitor内部包含两个重要队列:等待集(Wait Set)和入口列表(Entry List)。Wait Set用于线程的条件等待和协作,线程调用`wait()`后进入此集合,通过`notify()`或`notifyAll()`唤醒。Entry List则管理锁的竞争,未能获取锁的线程在此排队,等待锁释放后重新竞争。理解两者区别有助于设计高效的多线程程序。 - **Wait Set**:线程调用`wait()`后进入,等待条件满足被唤醒,需重新竞争锁。 - **Entry List**:多个线程竞争锁时,未获锁的线程在此排队,等待锁释放后获取锁继续执行。
35 12
|
4天前
|
存储 安全 Java
Java多线程编程秘籍:各种方案一网打尽,不要错过!
Java 中实现多线程的方式主要有四种:继承 Thread 类、实现 Runnable 接口、实现 Callable 接口和使用线程池。每种方式各有优缺点,适用于不同的场景。继承 Thread 类最简单,实现 Runnable 接口更灵活,Callable 接口支持返回结果,线程池则便于管理和复用线程。实际应用中可根据需求选择合适的方式。此外,还介绍了多线程相关的常见面试问题及答案,涵盖线程概念、线程安全、线程池等知识点。
46 2
|
28天前
|
Java 数据库
java小工具util系列1:日期和字符串转换工具
java小工具util系列1:日期和字符串转换工具
55 26
|
27天前
|
设计模式 Java 开发者
Java多线程编程的陷阱与解决方案####
本文深入探讨了Java多线程编程中常见的问题及其解决策略。通过分析竞态条件、死锁、活锁等典型场景,并结合代码示例和实用技巧,帮助开发者有效避免这些陷阱,提升并发程序的稳定性和性能。 ####
|
27天前
|
缓存 Java 开发者
Java多线程编程的陷阱与最佳实践####
本文深入探讨了Java多线程编程中常见的陷阱,如竞态条件、死锁和内存一致性错误,并提供了实用的避免策略。通过分析典型错误案例,本文旨在帮助开发者更好地理解和掌握多线程环境下的编程技巧,从而提升并发程序的稳定性和性能。 ####
|
20天前
|
安全 算法 Java
Java多线程编程中的陷阱与最佳实践####
本文探讨了Java多线程编程中常见的陷阱,并介绍了如何通过最佳实践来避免这些问题。我们将从基础概念入手,逐步深入到具体的代码示例,帮助开发者更好地理解和应用多线程技术。无论是初学者还是有经验的开发者,都能从中获得有价值的见解和建议。 ####