Java第十次作业


1.编写一个方法,实现冒泡排序(由小到大),并调用该方法

package homework;

public class Home5 {
    public static void main(int x[] ) {
//        1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
        int a = 0;
        for (int i = x.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (x[j] > x[i]) {
                    a = x[j];
                    x[j] = x[i];
                    x[i] = a;
                }

            }

        }

    }

    public static void main(String[] args) {
        int x[] = { 9, 5, 7, 2, 3, 1, 10 };
        main(x);
        for (int i : x) {
            System.out.print(i + " ");
        }
    }
}

  

 

 

 2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]

  

package week10;

public class Jc {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum1=jc(5);
        System.out.println(sum1);
    }
    public static int jc(int n){
        int sum=1;
        for (int i =1; i <=n; i++) {
            sum*=i;
        }
        return sum;
    }
}

  

 

  3.编写一个方法,判断该年份是平年还是闰年。[必做题]

 

 

 

package test;

import java.util.Scanner;

public test {

    public static void lq(){
        // TODO 自动生成的方法存根
        System.out.println("请输入一个年份");
        bc();
    }
    public static void bc() {
        Scanner input=new Scanner(System.in);
        int a=input.nextInt();
        if(a%4==0||a%400==0&&a%100!=0)
            System.out.println("是闰年");
            else 
                System.out.println("不是闰年");
        }
        public static void main(String[] args){
            lq();
        
    }
}

  

4.课堂没完成的menu菜单,实现幸运抽奖功能

import java.util.Scanner;


public class test  {
        
    
    
        
        public static void px() {
            Scanner input = new Scanner(System.in);
            System.out.println("输入5个数:");
            int a[] = new int[5];
            int b;
            for (int i = 0; i < a.length; i++) {
                a[i] = input.nextInt();
            }
            for (int i = 0; i < a.length - 1; i++) {
                for (int j = i+1; j < a.length; j++) {
                    if (a[i] > a[j]) {
                        b = a[i];
                        a[i] = a[j];
                        a[j] = b;
                    }
                }
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]);
                System.out.print("  ");
            }
            System.out.println();
        }
        public static void main(String[] args) {
            px();
        }
    }

  

 

posted @ 2021-05-12 15:43  崔宝元  阅读(49)  评论(0编辑  收藏  举报