结果为什么不对呢

abcjavabc 2013-12-03 10:31:30
import java.util.*;
public class Password {
public static void main(String[] args ){
Scanner input=new Scanner(System.in);
System.out.println("请输入你的密码:");
for(int i=0;i<100;){
String password=input.next();
if(password!="123456"){
System.out.println("error");
i++;
if(i==3)
{
System.out.println("对不起你输入的次数大于3次,稍后再试");
}
continue;
}
else {
System.out.println("right");
break;
}
}
}
}
...全文
268 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
___________小P 2013-12-04
  • 打赏
  • 举报
回复
if (!password .equals("123456"))
蟹道人 2013-12-04
  • 打赏
  • 举报
回复
引用 9 楼 magi1201 的回复:
突然发现这个挺好玩的,哈哈,可以让休息几秒钟接着输,也可以直接跳出

public static void main(String[] args ) throws InterruptedException{
    Scanner input = new Scanner(System.in);
    System.out.println("请输入你的密码:");
    int i= 0;
    while (true) {
        String password = input.next();
        if (!"123456".equals(password)) {
	    System.out.println("error");
	    System.out.println("请输入你的密码:");
	    i++;
	    if (i%3 == 0) {
	        System.out.println("对不起你已连续输错3次密码,请稍后再试");
//		Thread.sleep(4000);
//		System.out.println("请输入你的密码:");
		// 也可直接跳出
		break;
	    }
	} else {
	    System.out.println("right");
            break;
	}
    }
}
支持,用线程应该可以使用楼主想用的效果了。
末日哥 2013-12-04
  • 打赏
  • 举报
回复
if(password!="123456") 改成 if (!"123456".equals(password)) i==3 改成i%3=0或者在if里将i置为0
帝瓜 2013-12-04
  • 打赏
  • 举报
回复
字符串型用equal不用!=
姜小白- 2013-12-03
  • 打赏
  • 举报
回复
突然发现这个挺好玩的,哈哈,可以让休息几秒钟接着输,也可以直接跳出

public static void main(String[] args ) throws InterruptedException{
    Scanner input = new Scanner(System.in);
    System.out.println("请输入你的密码:");
    int i= 0;
    while (true) {
        String password = input.next();
        if (!"123456".equals(password)) {
	    System.out.println("error");
	    System.out.println("请输入你的密码:");
	    i++;
	    if (i%3 == 0) {
	        System.out.println("对不起你已连续输错3次密码,请稍后再试");
//		Thread.sleep(4000);
//		System.out.println("请输入你的密码:");
		// 也可直接跳出
		break;
	    }
	} else {
	    System.out.println("right");
            break;
	}
    }
}
时光清浅 2013-12-03
  • 打赏
  • 举报
回复
if(password!="123456") 改成 if (!"123456".equals(password))
姜小白- 2013-12-03
  • 打赏
  • 举报
回复
引用 6 楼 magi1201 的回复:

public static void main(String[] args ){
    Scanner input = new Scanner(System.in);
    System.out.println("请输入你的密码:");
    int i= 0;
    while (true) {
        String password = input.next();
        if (!"123456".equals(password)) {
            System.out.println("error");
	    i++;
	    if (i == 3) {
	        System.out.println("对不起你输入的次数大于3次,稍后再试");
	    }
	    // continue;
	 } else {
	     System.out.println("right");
	     break;
	 }
    }
}
格式木有调好,楼主将就看吧
姜小白- 2013-12-03
  • 打赏
  • 举报
回复

public static void main(String[] args ){
    Scanner input = new Scanner(System.in);
    System.out.println("请输入你的密码:");
    int i= 0;
    while (true) {
        String password = input.next();
        if (!"123456".equals(password)) {
            System.out.println("error");
	    i++;
	    if (i == 3) {
	        System.out.println("对不起你输入的次数大于3次,稍后再试");
	    }
	    // continue;
	 } else {
	     System.out.println("right");
	     break;
	 }
    }
}
时光清浅 2013-12-03
  • 打赏
  • 举报
回复
大半夜楼主还在学习,精神可赞,有啥不明白的引用我
淡淡的蛋蛋 2013-12-03
  • 打赏
  • 举报
回复
你这里怎么能用!=比较2个字符串呢。。。比较字符串要用equals
abcjavabc 2013-12-03
  • 打赏
  • 举报
回复
这样还是没达到要的效果啊。。
淡淡的蛋蛋 2013-12-03
  • 打赏
  • 举报
回复
if (!password .equals("123456")) 这样写
dyflovexlp 2013-12-03
  • 打赏
  • 举报
回复
字符串比较,用equals !"123456".equals(password)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧