A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fmi110 高级黑马   /  2015-8-21 09:10  /  299 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

d
  1. package string;

  2. import java.util.Scanner;

  3. import fmi110.Game;


  4. public class Test1 {

  5.         /**
  6.          * 模拟登陆系统
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 //新建原始密码
  12.                 String password = "123456";
  13.                 //设置输入次数
  14.                 int count = 3;
  15.                 //建立键盘接入的密码
  16.                 Scanner sc = new Scanner(System.in);
  17.                 System.out.println("输入密码,还有"+count+"次机会:");
  18.                 while((count--)>0){
  19.                         String code = sc.nextLine();//输入的密码
  20.                         if(password.equals(code)){
  21.                                 System.out.println("输入正确!!猜数字游戏吧!");
  22.                                 Game.start();
  23.                                 break;
  24.                         }
  25.                         else{
  26.                                 if(count>0)
  27.                                         System.out.println("输入错误,还有"+count+"次机会:");
  28.                                 else
  29.                                         System.out.println("登陆失败!!");
  30.                         }
  31.                 }

  32.         }

  33. }
  34. //猜数字游戏
  35. public class Game {
  36.        
  37.         public static void start(){
  38.                         int time = 5;
  39.                         System.out.println("请输入数字,还有"+time+"次机会");
  40.                         int num = (int)(Math.random()*100 +1);//随机产生数字
  41.                         for(;time>0;){
  42.                                 time--;
  43.                                 Scanner sc = new Scanner(System.in);
  44.                                 int gnum ;
  45.                                 gnum = sc.nextInt();
  46.                                 if(gnum > num)
  47.                                         System.out.println("输入大了,还有"+time+"次机会:");
  48.                                 else if(gnum < num)
  49.                                         System.out.println("输入小了,还有"+time+"次机会:");
  50.                                 else
  51.                                         System.out.println("恭喜!!");
  52.                         }
  53.                         System.out.println("Game Over!!");
  54.         }
  55. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马