java编程思想第四版第十三章字符串 习题
时间:2022-07-25
本文章向大家介绍java编程思想第四版第十三章字符串 习题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
第二题
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);
}
}
第四题
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 + "sn";
public final String PRICE_FORMAT = "%-" + ITEM_WIDTH + "."
+ PRICISION_String_WIDHT + "s %" + QTY_WIDTH + "d %" + PRICE_WIDTH
+ "." + PRICISION_FLOAT_WIDHT + "fn";
public final String TOTAL_FORMAT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH
+ "s %" + PRICE_WIDTH + "." + PRICISION_FLOAT_WIDHT + "fn";
//打印标题
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
第五题
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 + "sn";
public final String PRICE_FORMAT = "%-" + ITEM_WIDTH + "."
+ PRICISION_String_WIDHT + "s %" + QTY_WIDTH + "d %" + PRICE_WIDTH
+ "." + PRICISION_FLOAT_WIDHT + "fn";
public final String TOTAL_FORMAT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH
+ "s %" + PRICE_WIDTH + "." + PRICISION_FLOAT_WIDHT + "fn";
//打印标题
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
第七题:
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
第八题
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);
}
}
第九题
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, "_"));
}
}
第十题
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.ws+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
第十一题
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
- fas
- fa
- afd
- sfa
- f
- a
- fda
- sdf
- af
- a
- fd
- af
- as
- fd
- asfd
- af
- da
- f
- df
- as
- fa
- sf
- asf
- a
- sf
- af
- asf
- 【译】TensorFlow实现Batch Normalization
- 关于Python语言规范你需要知道的一些小tips
- R语言可视化——REmap(路径图)
- python面向对象
- 字符串hash入门
- R语言数据处理——数据合并与追加
- python爬取链家租房之获取房屋页面的详细信息(房名,地址,房价,面积,url)
- 信用卡“坏账”客户分析(二)
- Mac上提升python运算速度-PyPy初体验
- python处理json数据(复杂的json转化成嵌套字典并处理)
- Pointer-network理论及tensorflow实战
- python3 log文件处理获取某天需要的数据
- 实战深度强化学习DQN-理论和实践
- Python 工匠:编写条件分支代码的技巧
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Linux gdb+valgrind调试
- matplotlib基础绘图命令之hist
- Python|一文详解数据预处理
- 安排上了!PC人脸识别登录,出乎意料的简单
- valgrind使用介绍
- 实用!一键生成数据库文档,堪称数据库界的Swagger
- 算法集锦(2)|scikit-learn| 如何利用文本挖掘推荐Ted演讲
- 算法集锦(3)|采用医疗数据预测糖尿病的算法
- 谁说Cat不能做链路跟踪的,给我站出来
- Libra:一种Python工具,可以用几行代码自动实现机器学习过程
- 国内首个“新基建”安全大赛启动了!
- Kubernetes 中 Informer 的使用
- 嵌入式开发中常见3个的C语言技巧
- 恕我直言,我也是才知道ElasticSearch条件更新是这么玩的
- 有了MinIO,你还会用FastDFS么?