java语言程序设计 第十二章 (12.15、12.16、12.17)
来源:互联网 发布:淘宝99大促 编辑:程序博客网 时间:2024/06/14 08:43
程序小白,希望和大家多交流,共同学习
import java.util.Scanner;import java.util.Random;import java.util.Arrays;import java.io.PrintWriter;import java.io.File;public class ReadNumberData{ public static void main(String [] args) throws Exception { File file = new File("Exercise12_15.txt"); PrintWriter output = null; if (!file.exists()) { output = new PrintWriter(file); } Random random = new Random(7); for (int i = 0; i < 100; i++) { output.print(random.nextInt(100) + " "); } output.close(); Scanner input = new Scanner(file); int[] numbers = new int[100]; int index = 0; while (input.hasNext()) { numbers[index++] = input.nextInt(); input.skip(" "); } input.close(); Arrays.sort(numbers); for (int number : numbers) { System.out.println(number); } }}
import java.util.Scanner;import java.util.ArrayList;import java.io.File;import java.io.PrintWriter;public class ReplaceInitialTxt{ public static void main(String [] args) throws Exception { if (args.length != 3) { System.out.println("Useage: filename oldstring newstring"); System.exit(1); } File file = new File(args[0]); if (!file.exists()) { System.out.println("File " + args[0] + " is not exist"); System.exit(3); } Scanner input = new Scanner(file); ArrayList<String> list = new ArrayList<>(); while (input.hasNextLine()) { String str = input.nextLine(); list.add(str.replaceAll(args[1], args[2])); } try( PrintWriter output = new PrintWriter(file); ) { for (String str : list) { output.println(str); } } }}
import java.util.Scanner;import java.util.ArrayList;import java.util.Arrays;import java.io.File;import java.io.PrintWriter;public class Executioner{ public static void main(String [] args) throws Exception { File file = new File("hangman.txt"); if (!file.exists()) { System.out.println("file is not exist"); System.exit(1); } ArrayList<String> list = new ArrayList<>(); Scanner input = new Scanner(file); while (input.hasNext()) { list.add(input.next()); input.skip(" "); } int index = (int)(Math.random() * (list.size())); int times = 0; String word = list.get(index); char[] guess = new char[word.length()];; Arrays.fill(guess, '*'); // +fill(a : char[], ch: char): void Scanner in = new Scanner(System.in); while (result(guess)) { System.out.print("(Guess) Enter a letter in word " + Arrays.toString(guess) + " >"); char ch = in.next().charAt(0); int addResult = add(word, guess, ch); // 已经存在返回 -1 // 单词中不存在此字符返回 -2 // 正确的就直接返回添加的位置 if (addResult == -1) { System.out.println(ch + " is already in the word"); } else if (addResult == -2) { System.out.println(ch + " is not in the word"); } times++; } System.out.println("The word is " + word + " You missed " + times + " time"); } public static boolean result(char[] guess) { for (int i = 0; i < guess.length; i++) { if (guess[i] == '*') { return true; } } return false; } public static int add(String word, char[] guess, char ch) { boolean hasExist = false; boolean exist = false; for (int i = 0; i < word.length(); i++) { if (ch == guess[i]) { hasExist = true; } if ((word.charAt(i) == ch) && (guess[i] == '*')) { guess[i] = ch; hasExist = false; return i; } } if (hasExist) { return -1; } else if (!exist) { return -2; } return 0; }}
阅读全文
1 0
- java语言程序设计 第十二章 (12.15、12.16、12.17)
- java语言程序设计 第十二章(12.7、12.8、12.9)
- java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
- java语言程序设计 第十二章 (12.18、12.19、12.20、12.21、12.22)
- java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- java 语言程序设计 第十二章(12.1、12.2、12.3、12.4、12.5、12.6)
- java语言程序设计基础篇第十二章编程练习题
- Java 语言程序设计基础篇原书第八版_第十二章_第八题_程序分享
- java语言程序设计 第八章(8.35、8.36)
- java语言程序设计 第七章(7.21、7.35)
- java语言程序设计 第十一章(11.8 11.9)
- java语言程序设计 第十三章 (13.6、13.7)
- java语言程序设计 第十三章 (13.17、13.18)
- chapter1(JAVA语言程序设计)
- Java语言程序设计(序)
- java语言程序设计 第十一章 11.3
- Java语言程序设计 第13章
- github上fork了别人的项目后,再同步更新别人的提交
- Windows去除显示EFI分区
- Angular 2-组件
- python3 使用静态属性节约对象实例的内存开销
- linux shell之数字加减
- java语言程序设计 第十二章 (12.15、12.16、12.17)
- HBase在滴滴出行的应用场景和最佳实践
- 数位dp总结
- Oracle开机自启
- jsp页面中添加img标签图片显示为一个叉的解决思路
- acm icpc 沈阳站简记
- linux 挂载nfts U盘或着移动硬盘
- 2017年10月24日 第十五次总结
- 汇编stos所遇问题