java字符串比较不匹配

时间:2013-11-25 01:14:51

标签: java string-comparison

下面是我的编码:

package functiontest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FunctionTest {
public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
    int option=0;
    System.out.println("Please enter your position:");
    System.out.println("\n\t1.\tAdministrator");
    System.out.println("\t2.\tManagement");
    System.out.println("\t3.\tClerk");
    System.out.println("\t4.\tEngineer");
    System.out.println("\t5.\tExit system");
    System.out.print("\n\tYour position:\t");
    do
    {
        option = scan.nextInt();
        if(option<1 || option > 5) System.out.print("You have enter wrong position" + 
"\nPlease choose your position again[1...5]:\t");
    }while(option<1 || option > 5);

    switch(option)
    {
        case 1:administrator();break;
        case 2:System.exit(0);break;
    }


}
public static void administrator()
{
    Scanner ascan = new Scanner(System.in);
    String username="administrator";
    String password="password";
    String iusername, ipassword;

    do{
        System.out.print("Please enter username:\t");
        iusername = ascan.nextLine();
        System.out.println("Your username:\t" + iusername);
        System.out.print("Please enter password:\t");
        ipassword = ascan.nextLine();
        System.out.println("The password you enter:\t" + ipassword);
        if(iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password))
        {
            System.out.println("you have enter wrong username or password");
            System.out.println("The system will exit");
            System.exit(0);
        }
    }while(iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password));

        int option;
        System.out.println("Login successful!");
        System.out.println("1. \tadd clerk");
        System.out.println("2. \tdelete clerk");
        System.out.println("3. \tadd engineer");
        System.out.println("4. \tdelete engineer");
        option = ascan.nextInt();ascan.nextLine();
}

}

首先我指定了我的用户名和密码:

    String username="administrator";
    String password="password";

中的用户密钥中读取用户名和密码
iusername = ascan.nextLine();
ipassword = ascan.nextLine();

最后在密码和用户名之间进行比较:

iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password)

这是我运行程序后的结果:

run:
Please enter your position:

1.  Administrator
2.  Management
3.  Clerk
4.  Engineer
5.  Exit system

Your position:  1
Please enter username:  administrator
Your username:  administrator
Please enter password:  password
The password you enter: password
you have enter wrong username or password
The system will exit
BUILD SUCCESSFUL (total time: 22 seconds)

当我比较用户名和密码时,它总是说不匹配,在我尝试打印用户名和密码后,我确定我是正确的, 但程序总是说我输入了错误的用户名和密码,我可能知道我做错了吗?

3 个答案:

答案 0 :(得分:3)

看看你的代码。您已经说过“如果用户名和密码正确,请打印一条消息说他们错了,然后退出。”

答案 1 :(得分:2)

而不是

if(iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password))

你想要

if(!iusername.equalsIgnoreCase(username) || !ipassword.equalsIgnoreCase(password))

如果用户名或密码不正确,您的目标是打印错误消息,如果两者都不正确,则打印错误消息。

答案 2 :(得分:1)

在第一次迭代中退出循环。比较工作正常,但是你告诉程序打印错误信息,即使它是正确的。

 if(iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password))
    {
        System.out.println("you have enter wrong username or password");
        System.out.println("The system will exit");
        System.exit(0);
    }

你可能想要

 if(!iusername.equalsIgnoreCase(username) || !ipassword.equalsIgnoreCase(password))

添加! s

修改

您对循环的使用是POINTLESS。如果你不打算循环,那么只需取出循环部分。如果系统退出输入错误,循环的目的是什么?

如果您不希望它在有效输入上循环并在无效输入时退出

,那么您需要这样做
    System.out.print("Please enter username:\t");
    iusername = ascan.nextLine();
    System.out.println("Your username:\t" + iusername);
    System.out.print("Please enter password:\t");
    ipassword = ascan.nextLine();
    System.out.println("The password you enter:\t" + ipassword);
    if(!iusername.equalsIgnoreCase(username) || !ipassword.equalsIgnoreCase(password))
    {
        System.out.println("you have enter wrong username or password");
        System.out.println("The system will exit");
        System.exit(0);
    }

忘记循环!