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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© L番茄X 中级黑马   /  2015-5-28 16:33  /  696 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//控制台登录.倒包.
import java.util.Scanner;
class Demo
{
  public static void main(String[]args)
{
   //定义登录的密码还有名字.
  String Uname = "hellokitty";
  String Upwb = "26624";
//系统给出了三次登录的机会.
int count = 3 ;
  while(true){
     //控制台输入.
  Scanner sc = new Scanner(System.in);
   //提示输入;
  System.out.println("请输入登录的名字:");
   //接收登录的名字.
  String Youname = sc.next();
  System.out.println("请输入登录密码:");
    String Youpwb = sc.next();
    //判断是否与系统的一样.
  if(Youname.equals(Uname)&& Youpwb.equals(Upwb)){
        //如果一样那就登录成功.
     System.out.println("登录成功!");
    break;
}else{
    count--;
    System.out.println("登录的名字或者密码错误,你还有"+count+"次机会");
   break;
    }
   }
  }
}   


6 个回复

倒序浏览
你的代码有问题。不知道你自己测试没。只能写错一次帐号密码,之后就没反应了,因为你break跳出了while循环,应该在while里加入条件 while(count>0)我把你的代码改一下你看一看:
  1. //控制台登录.倒包.
  2. import java.util.Scanner;
  3. class Test
  4. {
  5.           public static void main(String[]args)
  6.         {
  7.                    //定义登录的密码还有名字.
  8.                   String Uname = "hellokitty";
  9.                   String Upwb = "26624";
  10.                   //系统给出了三次登录的机会.
  11.                   int count = 3 ;
  12.                   while(count>0)
  13.                   {
  14.                              //控制台输入.
  15.                           Scanner sc = new Scanner(System.in);
  16.                            //提示输入;
  17.                           System.out.println("请输入登录的名字:");
  18.                            //接收登录的名字.
  19.                           String Youname = sc.next();
  20.                           System.out.println("请输入登录密码:");
  21.                           String Youpwb = sc.next();
  22.                             //判断是否与系统的一样.
  23.                           if(Youname.equals(Uname)&& Youpwb.equals(Upwb))
  24.                           {
  25.                                 //如果一样那就登录成功.
  26.                                   System.out.println("登录成功!");
  27.                                   break;
  28.                           }
  29.                           else
  30.                           {
  31.                                   count--;
  32.                                   if(count==0)
  33.                                   {
  34.                                           System.out.println("登录失败!");
  35.                                           break;
  36.                                   }
  37.                                   System.out.println("登录的名字或者密码错误,你还有"+count+"次机会");
  38.                           
  39.                           }
  40.                    }
  41.                   
  42.           }
  43. }
复制代码



回复 使用道具 举报
本帖最后由 曲终烟尽 于 2015-5-28 17:10 编辑

修改了两处,一个是while(count>0)
一个是
else语句里的 登录失败判断。
如果不要这个判断就还会输出一次
”还有0次机会“   这句不合时宜的话。。。
你也可以用for循环,更安全一些,while容易陷入死循环。
for(int i=0;i<count;i++){
你的代码.
}

for(int i=0;i<3;i++){}

点评

好  发表于 2015-5-28 17:32
回复 使用道具 举报
你这代码,只能运行一次,需要判断count而不是while(true)
回复 使用道具 举报
曲终烟尽 发表于 2015-5-28 17:06
你的代码有问题。不知道你自己测试没。只能写错一次帐号密码,之后就没反应了,因为你break跳出了while循环 ...

是.我漏了判断count了.
回复 使用道具 举报
我大黑马就是牛逼。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马