Java入门(3)__小程序集锦

打印等边三角

class Trianle{
    public static void main(String[] arges){
            int x = 1;
            while(x <= 10){
                int y = 1;
                while(y <= x){
                    System.out.print("* ");
                    y++;
                }
                System.out.println();
                x++;
                }
        }
}

这里写图片描述

打印正三角

class Trianle{
    public static void main(String[] arges){
            int x = 1;
            while(x <= 10){
                int z = 1;
                while(z <= 10-x){
                    System.out.print(" ");
                    z++;
                    }

                int y = 1;
                while(y <= x){
                    System.out.print("* ");
                    y++;
                }
                System.out.println();
                x++;
                }
        }
}

这里写图片描述

打印九九乘法表

class Nine{
    public static void main(String[] args){
        int line = 9;
        int row = 1;
        //行循环
        while(row <= 9){
            //列循环
            int col = 1 ;
            while(col <= 9){
                System.out.print(col + "*" + row + "=" + (col * row)+"\t");
                col ++;
                }
                row ++;
                System.out.println();
            }
        }
}

这里写图片描述


取一半


class Nine{
    public static void main(String[] args){
        int line = 9;
        int row = 1;
        //行循环
        while(row <= 9){
            //列循环
            int col = 1 ;
            while(col <= row){
                System.out.print(col + "*" + row + "=" + (col * row)+"\t");
                col ++;
                }
                row ++;
                System.out.println();
            }
        }
}

这里写图片描述

========================================

class Function{
    public static void main(String[] args){
    out("Hello World!!!!!");

    int a = 1;
    int b = 2;
    int c = add(a,b);
    System.out.println(c);

    //调用累加和
    System.out.println("sum(10)=" + sum(10))
    //调用table99
    pringNine();
    //阶乘
    fabric(5);
    }
    /*********************************
    *
    *
    *
    **********************************/
    //求两个数字和
    public static int add(int x, int y){
    return x+y;
    }
    //打印字符串
    public static void out(String mag){
    System.out.println(msg);
    }
    //max(a,b,c)求三个整数的最大值
    public static int max(int a,int b, int c){
    int max = a > b ? a : b;
    max = max > c? max : c;
    return max;
    }
    //sum(n)取累加和
    public static int sum(int n){
    int sum = 0;
    for(int i = 1 ; i <= n; i ++){
        sum = sum + i;
        }
        return sum;
    }
    //打印99乘法表
    public static void printNine(){
    int row = 9;
    for(int i = 1; i <= row ; i++){
        for(int j = 1;j <= i ; j ++){
            System.out.print(j + "x" + i + "=" + (j * i) + "\t");
            }
            System.out.print("\t\n");
    }
}
//打印n*n
    public static void printN(row){
    for(int i = 1; i <= row ; i++){
        for(int j = 1;j <= i ; j ++){
            System.out.print(j + "x" + i + "=" + (j * i) + "\t");
            }
            System.out.print("\t\n");
    }
    //打印阶乘
    public static int fabric(int n){
    if (n <= 0){
        return -1;
        }
        int res = 1;
        for(int i = 1 ; i <= n ; i++){
            res = res * i;
            }
            return res;
    }
    //递归求阶乘
    public static int fac(int n){
        if(n == 1){
        return 1;
        }
        return n * fac(n - 1);
        }
}

冒泡排序 增强for循环

class Bubble{
    public static void main(String[] args){
        int [] arr = {1,2,4,3,7,6,9};
        for(int i = arr.length - 1 ; i > 0; i--){
            for(int j = 0 ; j < i ; j ++){
                if(arr[j] > arr[j + 1]){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    }
                }
        }
        //增强for循环
        for(int x : arr){
            System.out.print(x + " ");
            System.out.println();
            }

        }
    }

这里写图片描述



//矩阵转置
public static rotate2DArr(){
    int [] [] arr ={{1,2,3},{4,5,6},{7,8,9}};
    for(int i = 0 ; i < arr.length ; i++){
        for(j = 0; j < arr[i].length; j ++){
            int temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_39381833/article/details/81557930