import java.util.Scanner;
class Test {
public static void main(String[] args) {
// * 需求:模拟登录,给三次机会,并提示还有几次。
// * 用户名和密码都是admin
for (int j = 0; j < 3; j++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String pasword = sc.nextLine();
if ("admin".equals(userName) && "admin".equals(pasword)) {
System.out.println("登陆成功,欢迎" + userName + "来到猜数字游戏世界");
if (true) {
Game.playGame();
break;
}
} else {
if (j < 2) {
System.out.println("您还有"+(2-j)+"次机会");
}else {
System.out.println("系统检测到您的账号存在账号多次登陆失败存在被盗安全隐患,账号已经被锁定!");
}
}
}
}
}
class Game {
public static void playGame() {
int Num = (int)(Math.random()*100) + 1;
/*
* 0-50(50)大 25-50(25)小 25-35(35)大
*/
int star = 0;
int end = 100;
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:"+star+"-"+end);
int guessNum = sc.nextInt();
if(Num > guessNum) {
System.out.println("您猜的数字"+guessNum+"小了");
star = guessNum;
}else if (Num < guessNum) {
System.out.println("您猜的数字"+guessNum+"大了");
end = guessNum;
}else {
System.out.println("恭喜你猜中了!!");
break;
}
}
}
}
|
|