冒泡排序和用for循环画菱形

 忘了当时刚开始学java编程时提到的冒泡排序和for循环画菱形怎么做了, 找了找以前的练习, 重新修改了一遍, 其实冒泡排序也是可以排列字符串和字符的, 

package com.test;

public class Test {

    public static void main(String[] args) {
        //String str = "我是中国好公民世上只有妈妈好";
        String str = "dkfhgsdfhgksjhfgkjhskfghksfhgkejappqpwoqpipwdfjxmcvnmbvfd";
        char[] arr = str.toCharArray();

        System.out.println("冒泡排序: ");
        //外面的for循环是对所有的数进行比较
        for(int a = 0; a < arr.length; a++) {
            //里面的for循环实际上是进行几次比较
            for(int b = 0; b < arr.length - 1; b++) {
                if(arr[b] > arr[b+1]) {
                    char n = arr[b+1];//中间量
                    arr[b+1] = arr[b];
                    arr[b] = n;
                }
            }
        }
        //输出结果
        for(int q = 0;q < arr.length;q++) {
            System.out.print(arr[q]+", ");
        }
    }

}

for循环画菱形这个东西其实挺好玩的, 还记得当时刚学了四天的时候不会画, 从网上求助, 某大神用swing写了一个窗口, 输入数字就可以直接显示菱形, 崇拜的不得了, 虽然现在不经常接触swing, 内部实现原理也能想出来了

public class Star {
    /**
     * @param num 想显示几层的菱形
     */
    public static void print(int num) {
        
        int n = num * 2 - 1;
        
        for(int i = 0; i < n; i += 2) {
            
            for(int x = i / 2; x < (n-1) / 2; x++) {
                
                System.out.print(" ");
            }
            
            for(int j = 0; j <= i; j++) {
                
                System.out.print("*");
            }
            System.out.println();
        }
        
        for(int i = 0; i < n - 1; i += 2) {
            
            for(int x = 0; x < (i+2) / 2; x++) {
                
                System.out.print(" ");
            }
            for(int j = n; j >= i + 3; j--) {
                
                System.out.print("*");
            }
            
            System.out.println();
        }
        
    }
    
    public static void main(String[] args) {
        
        Star.print(10);
    }
}

原文地址:https://www.cnblogs.com/wgbs25673578/p/5969814.html