Java第十周作业

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

import java.util.Scanner;


public class 作业1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("请输入五个数");
        int a[]=new int[5];
        for (int i = 0; i < a.length; i++) {
            a[i]=input.nextInt();
        }for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if (a[j]>a[j+1]) {
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        System.out.println("排序后为");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

}

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

import java.util.Scanner;


public class 作业2 {
    public static int jc (int a){
        int sum=1;
        for (int i =1 ; i <=a; i++) {
            sum*=i;
        }System.out.println(a+"的阶乘为:");
        return sum;
        }
    public static void main(String[] args){
        System.out.println(jc(5));
    }
}

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

import java.util.Scanner;


public class 作业3 {
    public static void pd(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入年份");
        int a=input.nextInt();
        if (a%4==0  &&  a%100!=0  ||  a%400==0) {
            System.out.println(a+"年为闰年");
        }else {
            System.out.println(a+"年为平年");
        }
    }
    
    public static void main(String[] args){
        // TODO Auto-generated method stub
        pd();
    }
}

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

import java.util.Random;
import java.util.Scanner;


public class 作业四 {
    public static void mainMean() {
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎使用本系统");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.幸运抽奖");
        System.out.println("4.退出系统");
        System.out.println("请选择:");
        int i = input.nextInt();
        switch (i) {
        case 1:
            login();
            break;
        case 2:
            register();
            break;
        case 3:
            huiyuan();
            break;
        }
    }
    //完成3 输入四位会员卡号 如果百位等于随机数,就是幸运会员。询问是否返回主菜单。
    
    public static void register(){
        Scanner input = new Scanner(System.in);
        System.out.println("输入用户名");
        String uname=input.next();
        System.out.println("输入密码");
        String pwd=input.next();
        System.out.println("注册成功");
        returnMain();
    }
    public static void huiyuan(){
        Random r=new Random();
        int a=r.nextInt(10);
        Scanner input=new Scanner(System.in);
        System.out.println("请输入四位数会员号");
        int b=input.nextInt();
        System.out.println("输出随机数为"+a);
        int c=b%1000/100;
        System.out.println("输入百位数为"+c);
        if(a==c){
            System.out.println("是幸运会员");
        }
        else{
            System.out.println("不是幸运会员");
        }
        returnMain();
    }

    public static void returnMain(){
        Scanner input = new Scanner(System.in);
        System.out.println("是否返回主菜单?Y/N");
        if(input.next().equalsIgnoreCase("Y"))
            mainMean();
        else
            System.out.println("谢谢使用");
    }

    public static void login() {
        Scanner input = new Scanner(System.in);
        System.out.println("输入用户名");
        String uname=input.next();
        System.out.println("输入密码");
        String pwd=input.next();
        if(uname.equals("admin")&&pwd.equals("admin"))
            System.out.println("成功");
        else
            System.out.println("失败");
        returnMain();    

    }

    public static void main(String[] args) {
        mainMean();
    }

}

posted @ 2021-05-12 16:50  柳云倩  阅读(47)  评论(0编辑  收藏  举报