猜数字游戏

1、功能

登录后才可以玩游戏
这里写图片描述

2、登录Login 类
public class Login {
    public static void main(String[] args) {
        String usename = "haha";
        String password = "123456";
        System.out.println("登录界面");

        for(int x = 0; x < 3; x++){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String name = sc.nextLine();
            System.out.println("请输入密码");
            String psd = sc.nextLine();
            if(name.equals(usename) && psd.equals(password)){
                System.out.println("登录成功,开始游戏");
                GuessNumberGame.strat();
                break;
            }else {
                if(x == 2){
                    System.out.println("你的登录次数已达到上限,被冻结了,请到我行办理解冻手续");
                }else {
                    System.out.println("登录失败"+ (2-x) + "次机会");
                }
            }
        }
    }
}

3、猜数字游戏GuessNumberGame 类

public class GuessNumberGame {

    private GuessNumberGame() {
        super();
        // TODO Auto-generated constructor stub
    }

    public static void strat(){
        int number = (int)(Math.random() * 100) + 1;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入(1-100)的数字:");
            int x = sc.nextInt();   
            if(x < number){
                System.out.println("你猜的数字" + x + "小了,游戏继续");
            }else if(x > number){
                System.out.println("你猜的数字" + x + "大了,游戏继续");
            }else {
                System.out.println("你猜的数字" + x + "对了,游戏结束");
                break;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值