Jump to content
新域网络技术论坛

Java学习笔记


Jamers
 Share

Recommended Posts

2019.04.13 学习内容:

1. Swap 变量返回问题

2. 字符串输入

3. 类似PHP中的魔法调用,用反射方式动态调用不同方法

 

import java.lang.reflect.*;
import java.util.Scanner;
import cn.lcfms.utils.*;

public class Main {

    public static void main(String[] args)
    {
        String opt = "";
        try {
            if (args.length > 0) {
                Vardump.print(args);
                opt = args[0].toLowerCase();
                System.out.printf("Operate: %s \n", opt);
            } else {
                System.out.printf("Option is empty");
                return;
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        try {
            java.lang.Class[] empty_class = new java.lang.Class[]{};
            java.lang.Object[] empty_object = new java.lang.Object[]{};
            Method method = Main.class.getDeclaredMethod(opt, empty_class);
            method.setAccessible(true);
            method.invoke(null, empty_object);
        }
        catch (NoSuchMethodException e) {
            System.out.println(e.toString());
            System.out.printf("Option '%s' is incorrect", opt);
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }

    }

    /**
     * 交换数值入口
     */
    private static void swap()
    {
        Integer a, b;
        a = 10;
        b = 20;
        System.out.printf("a = %d, b = %d\n", a, b);
        try {
            doSwap(a, b);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        System.out.printf("a = %d, b = %d\n", a, b);
    }

    /**
     * 交换数值处理过程
     * @param a
     * @param b
     * @throws Exception
     */
    private static void doSwap(Integer a, Integer b) throws Exception
    {
        // 请在这里实现交换过程
        int c = a;
        Integer cInteger = new Integer(c);

        Class<? extends Integer> aClass = a.getClass();
        Field avalue = aClass.getDeclaredField("value");
        avalue.setAccessible(true);
        avalue.set(a, b);

        Class<? extends Integer> bClass = b.getClass();
        Field bvalue = bClass.getDeclaredField("value");
        bvalue.setAccessible(true);
        bvalue.set(b, cInteger);
    }

    /**
     * 输入字符串
     */
    private static void input()
    {
        System.out.print("请输入你的字符串:");
        Scanner s = new Scanner(System.in);
        String val = s.nextLine();
        String outstr = "你输入的字符串为空";
        if (val.length() > 0) {
            outstr = String.format("你输入的字符串为:%s,长度为:%d", val, val.length());
        }
        System.out.println(outstr);
    }

    private static void rightnum()
    {
        System.out.println("run rightnum");
    }
}

 

Link to comment
Share on other sites

继续学习:

痛苦的,连基本的数组操作也需要自己写过程,字符串操作之类也很麻烦,其他语言有大量的内置函数真的很好。如果没有一个好的封装库的话,肯定会很慢,真的!

    /**
     * 素数
     */
    private static void prime()
    {
        int num;
        System.out.print("请输入一个整数:");
        try {
            Scanner sc = new Scanner(System.in);
            num = sc.nextInt();
            if (num > 1) {
                boolean isPrime = true;
                for (int i = 2; i < Math.sqrt(num); i++) {
                    if (num % i == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime) {
                    System.out.printf("%d 是素数\n", num);
                } else {
                    System.out.printf("%d 不是素数\n", num);
                }
            } else {
                System.out.println("输入的数字必须大于1");
            }
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    /**
     * 水仙花数
     */
    private static void asphodel()
    {
        String str;
        int s1, s2, s3, count = 0;
        for (int i = 100; i < 1000; i++) {
            str = String.valueOf(i);
            s1 = Integer.parseInt(new String(new char[]{str.charAt(0)}));
            s2 = Integer.parseInt(new String(new char[]{str.charAt(1)}));
            s3 = Integer.parseInt(new String(new char[]{str.charAt(2)}));
            if (i == Math.pow(s1, 3) + Math.pow(s2, 3) + Math.pow(s3, 3)) {
                count ++;
                System.out.print(i + "   ");
            }
        }
        System.out.printf("\n水仙花数共有 %d 个\n", count);
    }

    /**
     * 正则与时间
     */
    private static void regular()
    {
        String date;
        Date dt = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = format.format(dt);
        System.out.println(date);
        String p = "(\\d{4})-(\\d{2})-(\\d{2}) (\\d+):(\\d+):(\\d+)";
        Pattern r = Pattern.compile(p);
        Matcher m = r.matcher(date);
        if (m.find()) {
            String[] ary = {};
            for (int i = 0; i <= m.groupCount(); i++) {
                ary = pushArray(ary, m.group(i));
            }
            Vardump.print(ary);
        } else {
            System.out.println("NO MATCH");
        }
    }

    /**
     * 操蛋,连数组操作也要自己造轮子?
     * @param src
     * @param push
     * @return
     */
    private static String[] pushArray(String[] src, String push)
    {
        String[] tempArray = new String[src.length + 1];
        for (int i = 0;i < src.length; i++) {
            tempArray[i] = src[i];
        }
        tempArray[src.length] = push;
        return tempArray;
    }

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...