第八次课后java作业

1:完善menu注册功能,抽奖,看小故事功能

package finish;

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


public class ttt {


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

    public static void mainMenu() {
        Scanner input = new Scanner(System.in);
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.幸运抽奖");
        System.out.println("4.看小故事,");
        System.out.println("5.退出系统");
        System.out.println("请选择");
        int i = input.nextInt();
        switch (i) {
        case 1:
            login();
            break;
        case 2:
            zhuce();
            break;
        case 3:
            choujiang();
            break;
        case 4:
            gushi();
            break;
        case 5:
            tuichu();
            break;
        }
    }

    private static void tuichu() {
        System.out.println("谢谢使用");

    }

    private static void gushi() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择想看的故事");
        System.out.println("1.魔法山羊");
        System.out.println("2.温暖的圣诞节");
        System.out.println("3.长大的皮皮虾");
        int y = input.nextInt();
        switch (y) {
        case 1:
            System.out.println("从前有个老山羊,不怕艰难困苦,专心为森林里的动物们治病,感动了是天赐予他魔法,从此没有他治不好的病了");
            break;
        case 2:
            System.out.println("从前有个孤独的田鼠,他很孤独,马上要过圣诞节了,他很想让别的动物陪他过圣诞节,最后森林里的动物全部都来了,给他了一个惊喜");
            break;
        case 3:
            System.out.println("皮皮虾总是问妈妈,什么时候长大,妈妈说你换几次新衣服就长大了,换了一次又一次,直到皮皮虾身边出现一个小皮皮虾,他知道他长大了");
            break;

        }

    }

    private static void choujiang() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入四位会员卡号");
        int x = input.nextInt();
        Random r = new Random();
        int a = r.nextInt(10);
        if (x / 10 % 10 == a) {
            System.out.println("恭喜你中奖啦!");
        } else {
            System.out.println("谢谢参与");
        }
        returnMain();
    }

    private static void zhuce() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = input.next();
        System.out.println("请输入密码");
        String mima = input.next();
        System.out.println("注册成功");
        returnMain();
    }

    private 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("登录成功");
        } else {
            System.out.println("登录失败");
        }
        returnMain();
    }

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

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

package finish;

public class hhh extends ttt {
    
    public static void main(String[] args) {
        
            System.out.println(jc(5));
        }

        private static int jc(int a) {
            int p=1;
            for(int i=1;i<=a;i++){
                p*=i;    
            }
            return p;
        }

}

3.编写一个方法,判断该年份是平年还是闰年。

package finish;

public class hhh extends ttt {
    
    public static void main(String[] args) {
        
        year(2014);
    }

    private static void year(int a) {
        if (a % 4 == 0 || a % 100 != 0 && a % 400 == 0) {
            System.out.println("闰年");
        } else {
            System.out.println("不是闰年");
        }
    }
}

 

posted @ 2020-04-28 23:08  Day丶T  阅读(146)  评论(0编辑  收藏  举报