字符串比较,用equals!"123456".equals(password)

解决方案 »

  1.   

    if (!password .equals("123456"))
    这样写
      

  2.   

    你这里怎么能用!=比较2个字符串呢比较字符串要用equals
      

  3.   


    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;
     }
        }
    }
      

  4.   

    if(password!="123456") 改成 if (!"123456".equals(password))
      

  5.   

    突然发现这个挺好玩的,哈哈,可以让休息几秒钟接着输,也可以直接跳出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;
    }
        }
    }
      

  6.   

    if(password!="123456") 改成 if (!"123456".equals(password))
    i==3 改成i%3=0或者在if里将i置为0
      

  7.   

    if (!password .equals("123456"))