猜字小游戏

来源:互联网 发布:联通申请开通80端口 编辑:程序博客网 时间:2024/04/20 03:13
package com.luo;import java.util.Scanner;public class login {    public static void main(String[] args) {        String username = "luoluo";        String password = "123456";        for(int i = 0;i < 3;i++){        Scanner sc = new Scanner(System.in);        System.out.println("请输入用户名:");        String name = sc.nextLine();        System.out.println("请输入密码:");        String pwd = sc.nextLine();        if(name.equals(username) && pwd.equals(password)){            System.out.println("登录成功,开始猜字游戏吧");            PlayGussnumber.start();        }else{            if((2-i)==0){                System.out.println("你的账户被锁定了");            }else{            System.out.println("登录失败,你还有"+(2-i)+"次机会");;            }        }        }    }}
package com.luo;import java.util.Scanner;public class PlayGussnumber {    private PlayGussnumber(){}    public static void start(){        int number = (int)(Math.random()*100+1);        while(true){        System.out.println("请输入你要猜的数字(1-100):");        Scanner s = new Scanner(System.in);        int gussnumber = s.nextInt();        if(gussnumber>number){            System.out.println("你猜的数字"+gussnumber+"猜大了");        }else if(gussnumber<number){            System.out.println("你猜的数字"+gussnumber+"猜小了");        }else{            System.out.println("恭喜你,猜对了");            break;        }        }    }}

运行结果:

请输入用户名:
luoluo
请输入密码:

登录失败,你还有2次机会
请输入用户名:

请输入密码:

登录失败,你还有1次机会
请输入用户名:
luoluo
请输入密码:
123456
登录成功,开始猜字游戏吧
请输入你要猜的数字(1-100):
50
你猜的数字50猜小了
请输入你要猜的数字(1-100):
75
你猜的数字75猜大了
请输入你要猜的数字(1-100):
60
你猜的数字60猜小了
请输入你要猜的数字(1-100):
65
你猜的数字65猜大了
请输入你要猜的数字(1-100):
63
你猜的数字63猜大了
请输入你要猜的数字(1-100):
62
你猜的数字62猜大了
请输入你要猜的数字(1-100):
61
恭喜你,猜对了

原创粉丝点击