Java 编码

1、冒泡排序

public static void main(String[] args) {
        int[] arr = {99, 88, 11, 44, 55, 33};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int a = 1; a < arr.length - 1 - i; a++) {
                if (arr[a] > arr[a + 1]) {
                    int sum = arr[a];
                    arr[a] = arr[a + 1];
                    arr[a + 1] = sum;
                }
                for (int s : arr) {
                    System.out.print(s + " ");
                }
                System.out.println();
            }
        }
    }

2、九九乘表法

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for (int i = 1; i <= 9; i++) {
            for (int s = 1; s <= i; s++) {
                System.out.print(i + "*" + s + "=" + i * s);
            }
            System.out.println();
        }
    }

3、三角形

public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            for (int s = 1; s <= i; s++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

4、八大基本类型(byte的大小、默认值、数据范围)

static byte b;
      static short s;
      static int i;
      static long l;
      static float f;
      static double d;
      static char c;
      static boolean bo;
    public static void main(String[] args) {

                System.out.println("byte的大小:"+Byte.SIZE
                               +";默认值:"+b
                               +";数据范围:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE);

                System.out.println("short的大小:"+Short.SIZE
                               +";默认值:"+s
                               +";数据范围:"+Short.MIN_VALUE+" - "+Short.MAX_VALUE);

                 System.out.println("int的大小:"+Integer.SIZE
                               +";默认值:"+i
                               +";数据范围:"+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE);

                System.out.println("long的大小:"+Long.SIZE
                               +";默认值:"+l
                               +";数据范围:"+Long.MIN_VALUE+" - "+Long.MAX_VALUE);

                System.out.println("float的大小:"+Float.SIZE
                                +";默认值:"+f
                                +";数据范围:"+Float.MIN_VALUE+" - "+Float.MAX_VALUE);

                System.out.println("double的大小:"+Double.SIZE
                                +";默认值:"+d
                                +";数据范围:"+Double.MIN_VALUE+" - "+Double.MAX_VALUE);

                System.out.println("char的大小:"+Character.SIZE
                               +";默认值:"+c
                               +";数据范围:"+Character.MIN_VALUE+" - "+Character.MAX_VALUE);

               System.out.println("boolean的大小:"+Byte.SIZE
                               +";默认值:"+bo
                               +";数据范围:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE);

        }

5、大小写字母转换

	/* 
    	toUpperCase  转换大写
        toLowerCase  转换小写
	*/
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // 大写转小写
        System.out.println("请输入大写字母:");
        String a = input.next();
        String dx = a.toLowerCase();
        System.out.println(dx);

        // 大写转小写
        System.out.println("请输入小写字母:");
        String b = input.next();
        String xx = b.toUpperCase();
        System.out.println(xx);
    }

6、数组排序(正序/倒序)

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入五位学员的成绩:");
        int[] arr = new int[5];
        for (int i = 0; i <= arr.length - 1; i++) {
            arr[i] = input.nextInt();
        }
        Arrays.sort(arr);
        System.out.print("(升序)从小到大排列为:  ");
        for (int s : arr) {
            System.out.print(s + "\t");
        }
        System.out.print("\n(倒序)从大到小排列为:  ");
        for (int w = arr.length - 1; w >= 0; w--) {
            System.out.print(arr[w] + "\t");
        }
	}

7、计算1+2-3+4-5+6-7…+100=?的结果:

public static void main(String[] args) {
	String s = "1";
        Integer sum = 1;
        for (int i = 2; i <= 100; i++) {
            if (i % 2 == 0) {
                s += "+" + i;
                sum += i;
            } else {
                s += "-" + i;
                sum -= i;
            }
        }
        System.out.println(s + "=" + sum);
	}

8、Format 格式化字符串

public static void main(String[] args) {
        System.out.printf("%d \n", 56);     // %d 一个整数
        System.out.printf("%f \n", 2.56);   // %f 一个浮点数
        System.out.printf("%s \n", "你好呀!"); // %s 一个字符串
        System.out.printf("%c \n", '你');    // %c 一个字符
        System.out.printf("%b \n", "a");   //表示打印一个布尔类型
        //%后面加入-表示左对齐
        //%后面加入数字表示输出结果的宽度
    }

9、时间-LocalDateTime

public static void main(String[]args) {
         /*
            Local 当地的
            Date 日期
            Time 时间
            now 当前;现在
            of 指定时间;自定义时间
         */
        System.out.println("当前日期时间为:" + LocalDateTime.now());
        System.out.println("当前日期为:" + LocalDate.now());
        System.out.println("当前时间为:" + LocalTime.now());
        System.out.println("自定义时间为:" + LocalDateTime.of(2018, 12, 12, 02, 12));
        System.out.println("英文式格式:" + new Date());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值