《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二十一章练习题答案 下载本文

\{font-family: \\\New\\\sans-serif; font-size: 100%; color: black}\);

output.format(\,

\); output.format(\, \); output.format(\, \);

// Match the color in the text

// output.format(\ // output.format(\

// output.format(\bold}\

output.format(\, \);

output.format(\, \); output.format(\, \); output.format(\, \);

String text = \; String temp;

// Read all lines

while (input.hasNext()) { temp = input.nextLine(); text += temp + \; }

text = text.replaceAll(\, \); text = text.replaceAll(\, \); translateToHTML(text, input, output);

output.format(\, \); output.format(\, \); output.format(\, \); }

catch (Exception ex) { System.out.println(ex); }

finally { try {

input.close(); output.close(); }

catch (Exception ex) { } } }

/** Translate Java source code to HTML */

static void translateToHTML(String text, Scanner input, PrintWriter output) throws Exception {

text = text.replaceAll(\, \); text = text.replaceAll(\, \);

String token;

while (text != null && text.length() > 0) { // * and / are in conflict with /* and //

String[] parts = text.split(\, 2);

token = parts[0];

if (token.length() > 1 && token.startsWith(\)) { output.format(\, \); parts = text.split(\, 2); text = parts[1];

output.format(\, parts[0].replaceAll(\, \)); output.format(\, \); continue; }

else if (token.length() > 1 && token.startsWith(\)) { output.format(\, \); parts = text.split(\, 2); text = parts[1];

output.format(\, parts[0].replaceAll(\, \) + \);

output.format(\, \); continue; }

else if (token.length() > 1 && token.matches(\)) { output.format(\, \); output.format(\, token); output.format(\, \); }

else if (token.startsWith(\) && token.endsWith(\) && (token.length() > 1)) {

output.format(\, \ + token + \); }

else if (token.startsWith(\) && token.endsWith(\) && (token.length() > 1)) {

output.format(\, \ + token + \); }

else if (token.equals(\)) {

output.format(\, \ + token + \); }

else if (token.startsWith(\) && token.endsWith(\) && (token.length() == 1)) { if (stringToken) {

output.format(\, token + \); stringToken = false; } else {

output.format(\, \ + token); stringToken = true; } }

else if (token.startsWith(\)) {

output.format(\, \ + token); stringToken = true; }

else if (token.endsWith(\) && (!token.endsWith(\))) { output.format(\, token); output.format(\, \); stringToken = false; }

else if (token.matches(\)) { // Check if numeric output.format(\, \ + token + \); }

else if (!stringToken && keywordSet.contains(token)) { output.format(\, \ + token + \); } else {

output.format(\, token); }

if (token.length() < text.length()) { if (text.charAt(token.length()) == '<') output.format(\, \);

else if (text.charAt(token.length()) == '>') output.format(\, \); else

output.format(\, text.charAt(token.length())); }

if (parts.length == 2) { text = parts[1]; } } } }

21.6

import java.util.*;

public class Exercise21_06 {

public static void main(String[] args) {

// Create a tree map to hold words and key and count as value TreeMap treeMap = new TreeMap<>();

Scanner input = new Scanner(System.in); while (true) { // Enter an integer

System.out.print(\); int number = input.nextInt();

if (number == 0) break;

Integer key = new Integer(number);

if (treeMap.get(key) != null) {

int value = ((Integer)treeMap.get(key)).intValue(); value++;

treeMap.put(key, new Integer(value)); } else {

treeMap.put(key, new Integer(1)); } }