zoukankan      html  css  js  c++  java
  • 2020-07-28日报博客

    2020-07-28日报博客

    1.完成的事情:

    • 完成CodeGym Java基础level 9部分,并完成练习题
    • 阅读《大道至简》。

    2.遇到的问题:

    • Java中对异常处理的思想和方式,异常捕获和抛出的方式。

    3.明日计划:

    • 继续学习Java。
    /*taskKey="zh.codegym.task.task09.task0930"
    
    有关算法的任务
    
    任务:用户从键盘输入单词(和数字)列表。单词按升序显示,数字按降序显示。
    
    示例输入:
    Cherry
    1
    Bob
    3
    Apple
    22
    0
    Watermelon
    
    示例输出:
    Apple
    22
    Bob
    3
    Cherry
    1
    0
    Watermelon
    
    
    Requirements:
    1.	程序必须从键盘读取数据。
    2.	程序应在屏幕上显示数据。
    3.	显示的单词应按升序排列(使用提供的 isGreaterThan 方法)。
    4.	显示的数字必须按降序排列。
    5.	main 方法应使用 sort 方法。
    6.	sort() 方法应调用 isGreaterThan() 方法。
    7.	sort() 方法应调用 isNumber() 方法。*/
    
    
    package zh.codegym.task.task09.task0930;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    /* 
    有关算法的任务
    */
    
    public class Solution {
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            ArrayList<String> list = new ArrayList<String>();
            while (true) {
                String s = reader.readLine();
                if (s.isEmpty()) break;
                list.add(s);
            }
    
            String[] array = list.toArray(new String[list.size()]);
            sort(array);
    
            for (String x : array) {
                System.out.println(x);
            }
        }
    
        public static void sort(String[] array) {
            //字符串的排序
            for (int i = 0; i < array.length - 1; i++) {
                int minIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (!isGreaterThan(array[j], array[minIndex]) && !isNumber(array[j]) && !isNumber(array[minIndex])) {
                        minIndex = j;
                    }
                }
                if (minIndex != i) {
                    String temp = array[i];
                    array[i] = array[minIndex];
                    array[minIndex] = temp;
                }
            }
            //数字的排序
            for (int i = 0; i < array.length - 1; i++) {
                int maxIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (isNumber(array[j]) && isNumber(array[maxIndex])) {
                        if (Integer.parseInt(array[j]) > Integer.parseInt(array[maxIndex])) {
                            maxIndex = j;
                        }
                    }
                }
                if (maxIndex != i) {
                    String temp = array[i];
                    array[i] = array[maxIndex];
                    array[maxIndex] = temp;
                }
            }
        }
    
        // 字符串比较方法:‘a’大于‘b’
        public static boolean isGreaterThan(String a, String b) {
            return a.compareTo(b) > 0;
        }
    
    
        // 传递的字符串是数字吗?
        public static boolean isNumber(String s) {
            if (s.length() == 0) return false;
    
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                if ((i != 0 && c == '-') // 字符串包含连字符
                        || (!Character.isDigit(c) && c != '-') // 或不是数字并且不以连字符开头
                        || (i == 0 && c == '-' && chars.length == 1)) // 或为单个连字符
                {
                    return false;
                }
            }
            return true;
        }
    }
    
    package zh.codegym.task.task09.task0930;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    /* 
    有关算法的任务
    */
    
    public class Solution {
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            ArrayList<String> list = new ArrayList<String>();
            while (true) {
                String s = reader.readLine();
                if (s.isEmpty()) break;
                list.add(s);
            }
    
            String[] array = list.toArray(new String[list.size()]);
            sort(array);
    
            for (String x : array) {
                System.out.println(x);
            }
        }
    
        public static void sort(String[] array) {
            //字符串的排序
            for (int i = 0; i < array.length - 1; i++) {
                int minIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (!isGreaterThan(array[j], array[minIndex]) && !isNumber(array[j]) && !isNumber(array[minIndex])) {
                        minIndex = j;
                    }
                }
                if (minIndex != i) {
                    String temp = array[i];
                    array[i] = array[minIndex];
                    array[minIndex] = temp;
                }
            }
            //数字的排序
            for (int i = 0; i < array.length - 1; i++) {
                int maxIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (isNumber(array[j]) && isNumber(array[maxIndex])) {
                        if (Integer.parseInt(array[j]) > Integer.parseInt(array[maxIndex])) {
                            maxIndex = j;
                        }
                    }
                }
                if (maxIndex != i) {
                    String temp = array[i];
                    array[i] = array[maxIndex];
                    array[maxIndex] = temp;
                }
            }
        }
    
        // 字符串比较方法:‘a’大于‘b’
        public static boolean isGreaterThan(String a, String b) {
            return a.compareTo(b) > 0;
        }
    
    
        // 传递的字符串是数字吗?
        public static boolean isNumber(String s) {
            if (s.length() == 0) return false;
    
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                if ((i != 0 && c == '-') // 字符串包含连字符
                        || (!Character.isDigit(c) && c != '-') // 或不是数字并且不以连字符开头
                        || (i == 0 && c == '-' && chars.length == 1)) // 或为单个连字符
                {
                    return false;
                }
            }
            return true;
        }
    }
    
    /*taskKey="zh.codegym.task.task09.task0929"
    
    让代码发挥它的用武之地!
    
    任务:程序读取两个文件名。它将第一个文件复制到第二个文件名指定的位置。
    新任务:程序读取两个文件名。它将第一个文件复制到第二个文件名指定的位置。
    如果指定的文件(待复制)不存在,那么该程序应显示“文件不存在”并再次读取文件名,然后才读取目标文件名称。
    
    
    Requirements:
    1.	程序必须读取文件名。
    2.	main 方法必须处理 getInputStream 方法抛出的异常。如果发生异常,则应显示&ldquo;文件不存在&rdquo;。
    3.	程序必须将第一个文件的内容复制到第二个文件。
    4.	main 方法应调用 getInputStream 方法。
    5.	不要更改 getInputStream 方法。
    6.	main 方法应调用 getOutputStream 方法。
    7.	不要更改 getOutputStream 方法。*/
    
    
    
    
    package zh.codegym.task.task09.task0929;
    
    import java.io.*;
    
    /* 
    让代码发挥它的用武之地!
    */
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    
            String sourceFileName = reader.readLine();
            InputStream fileInputStream = null;
            try {
                fileInputStream = getInputStream(sourceFileName);
            } catch (IOException e) {
                System.out.println("文件不存在");
                sourceFileName = reader.readLine();
                fileInputStream = getInputStream(sourceFileName);
            }
    
    
            String destinationFileName = reader.readLine();
            OutputStream fileOutputStream = getOutputStream(destinationFileName);
    
            while (fileInputStream.available() > 0) {
                int data = fileInputStream.read();
                fileOutputStream.write(data);
            }
    
            fileInputStream.close();
            fileOutputStream.close();
        }
    
        public static InputStream getInputStream(String fileName) throws IOException {
            return new FileInputStream(fileName);
        }
    
        public static OutputStream getOutputStream(String fileName) throws IOException {
            return new FileOutputStream(fileName);
        }
    }
    
    /*taskKey="zh.codegym.task.task09.task0927"
    
    十只猫
    
    有一个含 String 变量 name 的 Cat 类。
    创建一个 Map<String, Cat> 并添加 10 只猫,用(name,Cat)对表示。
    从 Map 中获取所有猫的集,并显示在屏幕上。
    
    
    Requirements:
    1.	程序不得从键盘读取数据。
    2.	createMap 方法必须创建一个新的 HashMap&lt;String, Cat&gt; 对象。
    3.	createMap 方法必须在映射中添加 10 只猫,用(名称,猫)对表示。
    4.	createMap 方法必须返回创建的映射。
    5.	convertMapToSet 方法必须创建并返回从传递的映射中检索到的猫的集。
    6.	程序应显示集中的所有猫。*/
    
    package zh.codegym.task.task09.task0927;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    /* 
    十只猫
    */
    
    public class Solution {
        public static void main(String[] args) {
            Map<String, Cat> map = createMap();
            Set<Cat> set = convertMapToSet(map);
            printCatSet(set);
        }
    
        public static Map<String, Cat> createMap() {
            Map<String, Cat> map = new HashMap<>();
            for (int i = 0; i < 10; i++) {
                Cat cat = new Cat("cat" + i);
                map.put(cat.name,cat);
            }
            return map;
        }
    
        public static Set<Cat> convertMapToSet(Map<String, Cat> map) {
            Set<String> setKey = map.keySet();
            Set<Cat> set = new HashSet<>();
            for (String s : setKey) {
                set.add(map.get(s));
            }
            return set;
        }
    
        public static void printCatSet(Set<Cat> set) {
            for (Cat cat : set) {
                System.out.println(cat);
            }
        }
    
        public static class Cat {
            private String name;
    
            public Cat(String name) {
                this.name = name;
            }
    
            public String toString() {
                return "Cat " + this.name;
            }
        }
    
    
    }
    
  • 相关阅读:
    样式表练习
    表单的制作
    PHP课后感
    eclipse中项目改名
    Windows下关闭占用指定端口应用程序的方法
    ftp上传不成功,提示501 invalid character in command错误
    分割字符串取最后一部分
    toString()方法之使用
    访问FTP服务器方法
    eclipse中svn检出项目,项目名上带有红色感叹号
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/13448938.html
Copyright © 2011-2022 走看看