Java题目

—冒泡排序

public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int [] nums=new int[5];
        System.out.println("请输入5个整数:");
        for (int i=0;i<nums.length;i++){
            nums[i]=input.nextInt();
        }
        System.out.println("排序后的结果是:");
        for (int i=0;i<nums.length-1;i++){
            for (int j=0;j<nums.length-1-i;j++){
                if (nums[j]>nums[j+1]){
                    int temp;
                    temp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                    break;
                }
            }
        }
        //打印输出
        for (int i=0;i<nums.length;i++){
            System.out.print(nums[i]+"\t");
        }
    }

—菱形

public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("请输入行数(偶数自动加1行):");
        int id=input.nextInt();
        id=id/2;
        for (int i=1;i<=id;i++){
            for (int j=0;j<=id-i;j++){
                System.out.print(" ");
            }
            for (int j=0;j<2*i-1;j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i=0;i<id+1;i++){
            for (int j=1;j<=i;j++){
                System.out.print(" ");
            }
            for (int j=2*id+1-i*2;j>0;j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }

—数据插入

public static void main(String[] args) {
        int [] ids={10,20,30,45,61,70,0};
        Scanner input=new Scanner(System.in);
        System.out.print("请输入要插入的数据:");
        int addid=input.nextInt();
        int index=ids.length-1;
        //查找下标
        for (int i=0;i<ids.length;i++){
            if (addid<ids[i]){
                index=i;
                break;
            }
        }
        //数据位移
        for (int i=ids.length-1;i>index;i--){
            ids[i]=ids[i-1];
        }
        //插入数据
        ids[index]=addid;
        //打印输出
        for (int i=0;i<ids.length;i++){
            System.out.print(ids[i]+"\t");
        }
    }

乘法表

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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页