zoukankan      html  css  js  c++  java
  • 快速求出2的三次方,插入一个值降序排列,空心菱形

    //快速求出2的三次方
    public class Clas1 {
        public static void main(String[] args) {
            System.out.println(2<<2);
        }
    }

     插入一个值降序排列

    import java.util.Scanner;
    
    public class Clas2 {
        public static void main(String[] args) {
            int[] scores = new int[6];//声明一个长度为6的数组
            scores[0] = 99;scores[1] = 60;scores[2] = 75;
            scores[3] = 72;scores[4] = 85;
            System.out.println("排序前");
            for(int score : scores) {
                System.out.print(score+" ");
            }
            System.out.println();
            System.out.println("排序后");
            //Arrays.sort(scores);//使用冒泡排序对数组进行降序排列
            for(int i=0;i<scores.length-1;i++) {
                for(int j=0;j<scores.length-i-1;j++) {
                    int temp = 0;
                    if(scores[j]<scores[j+1]) {
                        temp = scores[j];
                        scores[j] = scores[j+1];
                        scores[j+1] = temp;
                    }
                }
            }//查看降序排列之后的数组
            for(int score : scores) {
                System.out.print(score+" ");
            }
            System.out.println();
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学员的成绩:");
            int addScore = sc.nextInt();
            //用输入的成绩跟数组中每一个元素比较,当输入的成绩大于某个值时,
            //这个值的位置,就是输入的成绩应该在的位置,也就是找到输入成绩应该的下标
            int index = -1;
            for(int i=0;i<scores.length;i++) {
                if(addScore>scores[i]) {
                    index = i;//找到输入的成绩应该所在的下标
                    break;//保证插入的值插入正确地址
                }
            }
            //从倒数第二个值开始,每一个往后赋值,直到找到的下标
            for(int i = scores.length-2;i>=index;i--) {
                scores[i+1] = scores[i];
                //假设输入为77执行情况如下
                //      5              4
                //      4               3
                //      3               2//跳出
                //      2               1//不执行
            }
            //给找到的下标赋值为刚刚添加的成绩
            scores[index] = addScore;
            System.out.println("插入一个学员的成绩后:");
            for(int score : scores) {
                System.out.print(score+" ");
            }
            System.out.println();
        }
    }
    //空心菱形
    public class Clas3 {
        public static void main(String[] args) {
            for(int i=1;i<=5;i++) {//行数
                for(int j=1;j<=5-i;j++) {//控制空格
                    System.out.print(" ");
                }
                System.out.print("*");
                if(i!=1) {
                    for(int j=1;j<=2*i-3;j++) {//控制星号
                        System.out.print(" ");
                    }
                    System.out.print("*");
                }
                System.out.println();
            }
            
            for(int i=4;i>=1;i--) {//行数
                
                for(int j=1;j<=5-i;j++) {//控制空格
                    System.out.print(" ");
                }
                System.out.print("*");
                if(i!=1) {
                    for(int j=1;j<=2*i-3;j++) {//控制星号
                        System.out.print(" ");
                    }
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }
  • 相关阅读:
    服务器被黑

    ZXW说
    抽象类
    URL参数加密解密过程
    SqlServer 跨服务器 DML
    发布
    C#操作XML小结
    定时指执程序
    SQL语句判断数据库、表、字段是否存在
  • 原文地址:https://www.cnblogs.com/junge110/p/10485493.html
Copyright © 2011-2022 走看看