第十次

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

package TEST;

import java.util.Scanner;

public class Test {
    public static void n(int a[]) {
            for (int i = 0; i < a.length; i++) {
                for (int j =i+1; j < a.length; j++) {
                    if (a[i]>a[j]) {
                        int temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }

}
     public static void main(String[] args) {
         int a[]=new int[5];
         Scanner b=new Scanner(System.in);
         for (int i = 0; i < a.length; i++) {
            System.out.println("请输入第"+(i+1)+"个整数");
            a[i]=b.nextInt();
        }
         System.out.println("排序前:");
         for (int i = 0; i < a.length; i++) {
             System.out.print(a[i]+" ");
        }
         n(a);
         System.out.println();
         System.out.println("排序后:");
         for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        }

    }

 

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

 

 

package TEST;

import java.util.Scanner;

public class Test {
    private static int getSum(int n) {
        // TODO Auto-generated method stub
        if (n == 1) {
            return 1;
        }
        int temp = getSum(n - 1);
        return temp * n;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个整数");
        int n=input.nextInt();
        int sum = getSum(n);
        System.out.println("阶乘=" + sum);
    }

}

 

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

package TEST;

import java.util.Scanner;

public class Test {
    private static void year() {
        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("是闰年");
        } else {
            System.out.println("不是闰年");
        }

    }

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

}

 

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

package TEST;

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() {
        // 输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单
        Random r=new Random();
        int a=r.nextInt(10);
        Scanner input=new Scanner(System.in);
        System.out.println("输入四位会员卡号"); 
        int kahao= input.nextInt();
        System.out.println("幸运数字为"+a);
        while (kahao < 1000 || kahao > 10000) {
            System.out.println("输入错误,重新输入");
            kahao = input.nextInt();
            if (kahao >= 1000 && kahao < 10000) {
                break;
            }
        }
        int bai=kahao%1000/100;
        if (bai == a) {
            System.out.println("是幸运会员");
        } else {
            System.out.println("不是幸运会员");
        }

        returnMain();
    }


    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 on 2021-05-10 18:30  我裂开  阅读(18)  评论(0编辑  收藏  举报

导航