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

{\, \}, {\, \}, {\, \},

{\, \}, {\, \},

{\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \} };

Map map = new HashMap();

for (int i = 0; i < stateCapital.length; i++)

map.put(stateCapital[i][0].toLowerCase(), stateCapital[i][1]);

Scanner input = new Scanner(System.in); System.out.print(\);

String state = input.nextLine().toLowerCase().trim(); if (map.containsKey(state))

System.out.println(\ + map.get(state)); else

System.out.println(\); } }

21.10

import java.util.*; import java.io.*;

public class Exercise21_10 {

public static void main(String[] args) {

// Prompt the user to enter a Java source file

Scanner input = new Scanner(System.in); System.out.print(\); String filename = input.nextLine();

// Array of all Java keywords + true + null

String[] keywordString = { \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \ };

Set keywordSet = new

HashSet(Arrays.asList(keywordString)); int count = 0;

try {

input = new Scanner(new File(filename));

String text = \;

while (input.hasNext()) {

String line = input.nextLine(); line = stripLineComments(line); line = stripLineStringLiterals(line); text += line + \; }

text = stripParagraghComments(text);

TreeMap map = new TreeMap();

String[] tokens = text.split(\); for (String token : tokens) { if (keywordSet.contains(token)) if (map.get(token) == null) { map.put(token, 1); } else {

int value = map.get(token); value++;

map.put(token, value); } }

// Get all entries into a set

Set> entrySet = map.entrySet();

// Get key and value from each entry

for (Map.Entry entry: entrySet)

System.out.println(entry.getValue() + \ + entry.getKey()); } catch (Exception ex) { ex.printStackTrace(); } }

/* Strip line comments */

private static String stripLineComments(String line) { int index = line.indexOf(\);

if (index < 0) return line; else

return line.substring(0, index); }

/* Strip string literals */

private static String stripLineStringLiterals(String line) { int start = line.indexOf(\);

int end = line.indexOf(\, start + 1);

while (start > 0 && end > 0) {

line = line.substring(0, start) + line.substring(end + 1); start = line.indexOf(\); end = line.indexOf(\); }

return line; }

/* Strip paragraph comments */

private static String stripParagraghComments(String text) { int start = text.indexOf(\); int end = text.indexOf(\);

while (start > 0 && end > 0) {

text = text.substring(0, start) + text.substring(end + 2); start = text.indexOf(\);

end = text.indexOf(\); }

return text; } }

21.11

import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Scanner;

import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene;

import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage;

public class Exercise21_11 extends Application {

private Map[] mapForBoy = new HashMap[10]; private Map[] mapForGirl = new HashMap[10];

private Button btFindRanking = new Button(\); private ComboBox cboYear = new ComboBox<>(); private ComboBox cboGender = new ComboBox<>(); private TextField tfName = new TextField(); private Label lblResult = new Label();

@Override // Override the start method in the Application class public void start(Stage primaryStage) { GridPane gridPane = new GridPane();

gridPane.add(new Label(\), 0, 0); gridPane.add(new Label(\), 0, 1); gridPane.add(new Label(\), 0, 2); gridPane.add(cboYear, 1, 0); gridPane.add(cboGender, 1, 1); gridPane.add(tfName, 1, 2); gridPane.add(btFindRanking, 1, 3);