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;
}
}
}
}