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
原创粉丝点击