Java_200429

Posted 熊大To

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java_200429相关的知识,希望对你有一定的参考价值。

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

package ffeng;

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

public class Menu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            mainMenu();
            
    }

    private static void mainMenu() {
        // TODO Auto-generated method stub
        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("6:选择");
        int i = input.nextInt();
        switch (i) {
        case 1:
            logIn();
            break;
        case 2:
            register();
            break;
        case 3:
            luck();
            break;
        case 4:
            story();
            break;
        case 5:
            back();
            break;
        default:
            break;
        }
    }

    private static void back() {
        // TODO Auto-generated method stub
        System.out.println("再见了,我终于弄完了");
    }

    private static void story() {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("1:狼");
        System.out.println("2:蛇");
        System.out.println("3:小猪");
        System.out.println("选择");
         int a = input.nextInt();
         switch (a) {
        case 1:
            wolf();
            break;
        case 2:
            snake();
            break;
        default:
            pig();
            break;
        }
         mainMenu();
    }

    

    private static void pig() {
        // TODO Auto-generated method stub
        System.out.println("从前,有头猪,我不知道了");
        backMain();
    }

    private static void snake() {
        // TODO Auto-generated method stub
        System.out.println("蛇有毒,哈哈啊哈");
        backMain();
    }

    private static void wolf() {
        // TODO Auto-generated method stub
        System.out.println("我是一头狼,哈哈啊哈");
        backMain();
    }

    private static void luck() {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入4个数字");
         int a = input.nextInt();
         Random r = new Random();
         int i = r.nextInt(9);
         int k = r.nextInt(9);
         int q,s;
         q=a/1000;
         s=a%100/10;
         if(q==i&&s==k) {
            System.out.println("幸运顾客,奖1万块");
        }
         else{
             System.out.println("谢谢惠顾!");
         }
         mainMenu();
    }

    private static void register() {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入姓名");
        String uname = input.nextInt();
        Scanner input = new Scanner(System.in);
        System.out.println("输入性别");
        char sex= input.nextInt();
        System.out.println("输入年龄");
        int  age= input.nextInt();
        System.out.println("输入密码");
        long a = input.nextInt();
        System.out.println("注册成功");
        mainMenu();
    }

    private static void logIn() {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入姓名");
        String uname = input.nextInt();
        System.out.println("输入密码");
        String a = input.nextInt();
        if (uname.equals("xxl")&&a.equals(123456)) {
            System.out.println("登录成功");
        }
        else{
            System.out.println("登陆失败");
        }
        backMain();
    }

    private static void backMain() {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("是否返回主菜单");
        int i=input.nextInt();
        if (input.next().equalsIgnoreCase("Y")) {
            mainMenu();
        }
        else{
            System.out.println("谢谢使用");
        }
    }

}

 

 

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

package ffeng;

import java.util.Scanner;

public class f01 {

    public static void main(String[] args) {
        int sum=1;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int k = sc.nextInt();
        for (int i = 1; i <= k; i++) {
            sum=sum*i;
        }
        System.out.println(k+"的阶乘为:"+sum);
    }
}

 

以上是关于Java_200429的主要内容,如果未能解决你的问题,请参考以下文章

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

VSCode 配置 用户自定义代码片段 自定义自动代码补充

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

java代码在片段活动中不起作用

java 代码片段【JAVA】