Java语言程序设计与数据结构(基础篇)课后练习题 第十二章(三)

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()));
}

}

  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值