java字符串比较不匹配

java字符串比较不匹配,java,string-comparison,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 sc

下面是我的代码:

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();
}
从中的用户密钥读取用户名和密码

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)
当我比较用户名和密码时,它总是说不匹配,在我尝试打印我输入的用户名和密码后,我非常确定我是正确的,
但是程序总是说我输入了错误的用户名和密码,我可以知道我哪里做错了吗?

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

 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))
加上!

编辑:


使用循环是没有意义的。如果您不打算循环,那么只需取出循环部分。如果系统在输入错误时退出,循环的目的是什么

如果您不希望它在有效输入时循环并在无效输入时退出,那么您所需要的就是这个

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

忘记循环

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

而不是

if(iusername.equalsIgnoreCase(username) && ipassword.equalsIgnoreCase(password))
你想要

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

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

它没有更改任何内容,而是继续执行do while循环,条件为iusername.equalsIgnoreCaseusername&&ipassword.equalSignoreCasePassword您希望它做什么?用户名和密码应匹配为iusername=administrator ipassword=password,正如我所说,你使用循环是毫无意义的。如果您不打算循环,那么只需取出循环部分。如果系统在输入错误时退出,循环的目的是什么。请参阅我的编辑。
if(!iusername.equalsIgnoreCase(username) || !ipassword.equalsIgnoreCase(password))