d- package string;
- import java.util.Scanner;
- import fmi110.Game;
- public class Test1 {
- /**
- * 模拟登陆系统
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //新建原始密码
- String password = "123456";
- //设置输入次数
- int count = 3;
- //建立键盘接入的密码
- Scanner sc = new Scanner(System.in);
- System.out.println("输入密码,还有"+count+"次机会:");
- while((count--)>0){
- String code = sc.nextLine();//输入的密码
- if(password.equals(code)){
- System.out.println("输入正确!!猜数字游戏吧!");
- Game.start();
- break;
- }
- else{
- if(count>0)
- System.out.println("输入错误,还有"+count+"次机会:");
- else
- System.out.println("登陆失败!!");
- }
- }
- }
- }
- //猜数字游戏
- public class Game {
-
- public static void start(){
- int time = 5;
- System.out.println("请输入数字,还有"+time+"次机会");
- int num = (int)(Math.random()*100 +1);//随机产生数字
- for(;time>0;){
- time--;
- Scanner sc = new Scanner(System.in);
- int gnum ;
- gnum = sc.nextInt();
- if(gnum > num)
- System.out.println("输入大了,还有"+time+"次机会:");
- else if(gnum < num)
- System.out.println("输入小了,还有"+time+"次机会:");
- else
- System.out.println("恭喜!!");
- }
- System.out.println("Game Over!!");
- }
- }
复制代码
|
|