概述
12.15
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
File file = new File("Exercise12_15.txt");
if (!file.exists()) {
try (PrintWriter output = new PrintWriter(file);) {
for (int i = 1; i <= 100; i++)
output.print((int) (Math.random() * 100) + " ");
}
}
Scanner input = new Scanner(file);
int[] n = new int[100];
for (int i = 0; i < 100; i++)
n[i] = input.nextInt();
Arrays.sort(n);
for (int j = 0; j < 100; j++)
System.out.print(n[j] + " ");
}
}
12.16
看程序清单12-16去。
12.17
import java.io.File;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
File file = new File("hangman.txt");
if (!file.exists()) {
System.out.print("Not Exist。");
System.exit(1);
}
Scanner input = new Scanner(file);
String line = "";
while (input.hasNext()) {
line = input.nextLine();
}
String[] words = line.split(" ");
// 加上原先7.35的代码,注意题目要求用空格分隔,所以这里我就设定了一行,需要多行测试的话,要修改while语句的内容。
}
}
12.18
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Wrong!");
System.exit(1);
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println(args[0] + " is not exist!");
System.exit(2);
}
addPackage(file);
}
public static void addPackage(File file) throws Exception {
if (file.isFile()) {
add(file);
} else if (file.isDirectory()) {
File[] fileList = file.listFiles();
for (File file1 : fileList) {
addPackage(file);
}
}
}
public static void add(File file) throws Exception {
ArrayList<String> fileContent = new ArrayList<>();
Scanner input = new Scanner(file);
while (input.hasNext()) {
fileContent.add(new String(input.nextLine()));
}
try (PrintWriter output = new PrintWriter(file);) {
output.println("//" + file.getParent());
for (String content : fileContent) {
output.println(content);
}
}
}
}
12.19
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
URL url = new URL("http://liveexample.pearsoncmg.com/data/Lincoln.txt");
Scanner input = new Scanner(url.openStream());
ArrayList<String> list = new ArrayList<>();
while (input.hasNext()) {
list.add(input.nextLine());
}
int count = 0;
for (String l : list)
count += getCount(l);
System.out.println("Words: " + count);
}
public static int getCount(String s) {
int count = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (Character.isLetter(s.charAt(i)) && !(Character.isLetter(s.charAt(i + 1)))) {
count++;
}
}
return count;
}
}
12.20
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Wrong!");
System.exit(1);
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("Not Exist");
System.exit(2);
}
deleteWords(file);
}
public static void deleteWords(File file) throws Exception {
if (file.isFile()) {
delete(file);
} else {
File[] file1 = file.listFiles();
for (File f : file1)
deleteWords(f);
}
}
public static void delete(File file) throws Exception {
ArrayList<String> list = new ArrayList<>();
Scanner input = new Scanner(file);
while (input.hasNext()) {
list.add(input.nextLine());
}
if (list.get(0).equals("//" + file.getParent())) {
System.out.println("//" + file.getParent());
list.remove(0);
}
try (PrintWriter output = new PrintWriter(file);) {
for (String l : list)
output.println(l);
}
}
public static void print(ArrayList<String> list) {
for (String s : list)
System.out.println(s);
}
}
12.21
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
File file = new File("SortedStrings.txt");
if (!file.exists()) {
System.out.println("Wrong!");
System.exit(1);
}
Scanner input = new Scanner(file);
ArrayList<String> list = new ArrayList<>();
while (input.hasNext()) {
String[] s = input.nextLine().split("[,.]");
for (String s1 : s)
list.add(s1);
}
judge(list);
}
public static void judge(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++)
if (list.get(i).compareTo(list.get(i + 1)) > 0) {
System.out.println(list.get(i) + " " + list.get(i + 1));
break;
}
}
}
12.22
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Wrong!");
System.exit(1);
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("Not Exist!");
System.exit(2);
}
replaceText(file, args[1], args[2]);
}
public static void replaceText(File file, String oldString, String newString) throws Exception {
if (file.isFile()) {
replace(file, oldString, newString);
} else {
File[] fileList = file.listFiles();
for (File f : fileList) {
replaceText(f, oldString, newString);
}
}
}
public static void replace(File file, String oldString, String newString) throws Exception {
ArrayList<String> list = new ArrayList<>();
Scanner input = new Scanner(file);
while (input.hasNext()) {
list.add(input.nextLine().replaceAll(oldString, newString));
}
try (PrintWriter output = new PrintWriter(file);) {
for (String s : list) {
output.println(s);
}
}
}
}
12.23
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class dishierzhang {
public static void main(String[] args) throws Exception {
ArrayList<Integer> list = new ArrayList<>();
URL url = new URL("http://liveexample.pearsoncmg.com/data/Scores.txt");
Scanner input = new Scanner(url.openStream());
while (input.hasNext())
list.add(new Integer(input.nextInt()));
double sum = 0;
for (Integer i : list) {
sum += i;
System.out.print(i + " ");
}
System.out.println("Sum: " + sum);
System.out.print("Average: " + (sum / list.size()));
}
}
最后
以上就是懦弱热狗为你收集整理的Java语言程序设计与数据结构(基础篇)课后练习题 第十二章(三)12.1512.1612.1712.1812.1912.2012.2112.2212.23的全部内容,希望文章能够帮你解决Java语言程序设计与数据结构(基础篇)课后练习题 第十二章(三)12.1512.1612.1712.1812.1912.2012.2112.2212.23所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复