Java第九次作业

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

package work;

public class test {
    public static void main(String[] args) {
        int[] a = { 3, 9, 5, 45, 67, 99, 56 };
        test.mp(a);

    }

    public static void mp(int a[]) {
        for (int i = 1; i < a.length; i++) {
            for (int j = 0; j < a.length - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        for (int b : a) {
            System.out.print(b + " ");
        }
    }
}

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

package work;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        jc();
    }
    public static void jc() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入整数:");
        int a = input.nextInt();
        int jie=1;
        for (int i = 1;  i<=a; i++) {
            jie*=i;
            
        }
        System.out.println(jie);
    }
}

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

package work;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
     yp();
    }

    public static void yp() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = input.nextInt();
        if (year % 4 == 0 && year % 100 != 100 || year % 400 == 0) {
            System.out.println("该年份为闰年");
        } else {
            System.out.println("该年份为平年");
        }
    }
}

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

package work;

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

public class test {
        public static void mainMenu(){
            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:
                reg();
                break;
            case 3:
                lucky();
            
            }
            
            
        }
        
        private static void lucky() {
            System.out.println("输入四位卡号");
            Scanner input =new Scanner(System.in);
            int kh=input.nextInt();
            Random r=new Random();
            int a = r.nextInt(10);
            if(kh / 100 % 10==a) {
                System.out.println("该用户是幸运会员");
            }else {
                System.out.println("该用户不是幸运会员");
            }
        }

        public static void returnMain(){
            Scanner input=new Scanner(System.in);
            System.out.println("是否返回主菜单?");
            if(input.next().equalsIgnoreCase("Y"))
                mainMenu();
            else
                System.out.println("谢谢使用");
        }
        
        public static void reg() {
            // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            System.out.println("输入要注册的用户名");
            String uname=input.next();
            System.out.println("输入注册密码");
            String upwd=input.next();
            System.out.println("注册成功");
            returnMain();
            
            
        }


        public static void login(){
            Scanner input=new Scanner(System.in);
            System.out.println("输入用户名");
            String uname=input.next();
            System.out.println("输入密码");
            String upwd=input.next();
            if(uname.equals("zs")&&upwd.equals("123")){
                System.out.println("ok");
            }else{
                System.out.println("fail");
            }
            returnMain();        
        }
        

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

        }
    }

posted @ 2021-05-10 16:12  计算机1901万家乐  阅读(42)  评论(0编辑  收藏  举报