抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA 实验6.2

1:加密解密

package encrypt;

import java.util.Scanner;

public class Encrypt {

    public static void main(String[] args) {
        lock();
        unlock();
    }

    public static void lock() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();
        System.out.println("原字符串:" + s);
        StringBuilder ss = new StringBuilder(s);
        for (int i = 0; i < ss.length(); i++) {
            char c = ss.charAt(i);
            if ((c >= 'v' && c <= 'z') || (c >= 'V' && c <= 'Z')) {
                c = (char) (c - 21);
            } else {
                c = (char) (c + 5);
            }

            ss.setCharAt(i, c);
        }
        System.out.println("加密后的字符串是:" + ss);
    }

    public static void unlock() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个加密字符串:");
        String su = sc.nextLine();
        System.out.println("加密字符串是:" + su);
        StringBuilder ss = new StringBuilder(su);
        for (int i = 0; i < ss.length(); i++) {
            char c = ss.charAt(i);
            if ((c >= 'a' && c <= 'e') || (c >= 'A' && c <= 'E')) {
                c = (char) (c + 21);
            } else {
                c = (char) (c - 5);
            }

            ss.setCharAt(i, c);
        }
        System.out.println("原字符串是:" + ss);
    }
}

 

2:进制转换10to2

package encrypt;

import java.util.Scanner;

public class ToBinary {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个十进制数:");
        int ss = sc.nextInt();
        System.out.println("转化结果为:" + toBinary(ss));
    }

    public static String toBinary(int value) {
        String s = "";
        while (value != 0) {
            int r = value % 2;
            s = r + s;
            value = value / 2;
        }
        return s;
    }
}

 

3:命令行

package encrypt;

public class CommandLineDemo {

    public static void main(String[] args) {
        System.out.println("the command line has " + args.length + " arguments");
        for (int i = 0; i < args.length; i++) {
            System.out.println("argument number " + i + ";" + args[i]);
        }
    }
}

若尝试下列命令,结果又会怎样?

java CommandLineDemo /D 1024 /F test.dat

out:
the command line has 4 arguments
argument number 0;/D
argument number 1;1024
argument number 2;/F
argument number 3;test.dat

 

 


本站由以下主机服务商提供服务支持:

2条评论

发表评论