总是执行java中的最后一个语句

时间:2017-12-04 15:06:19

标签: java

当我运行代码时,它总是执行最后一个语句(admin)虽然如果我为不同的角色位置输入正确/不正确的用户ID或密码,只打开admin.java,无法打开其他语句

Scanner input1 = new Scanner(System.in);
System.out.println("User ID : ");
String salesid = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("Password : ");
String salespass = input2.next();

String smposition = "SM";
String pmposition = "PM";
String adposition = "AD";
{
    try
    {
        String line;
        File f = new File("UserDetails.txt");
        input1 = new Scanner(f);
        while(input1.hasNextLine())
        {
            String Line = input1.nextLine();
            String[] Seperator = Line.split(",");

            if(salesid.equals(Seperator[0]) && salespass.equals(Seperator[3]) && smposition.equals(Seperator[2]) || pmposition.equals(Seperator[2]) || adposition.equals(Seperator[2]))
            { 
                if (smposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    salesmanager ss = new salesmanager();
                    ss.sale();
                }
                else if(pmposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    purchasemanager pm = new purchasemanager();
                    pm.purchase();
                }
                else if(adposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    admin ad = new admin();
                    ad.adm();
                }
            }
        }
    }
    catch(IOException e)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

将您的if条件更改为

if(salesid.equals(Seperator[0]) && salespass.equals(Seperator[3]) && (smposition.equals(Seperator[2]) || pmposition.equals(Seperator[2]) || adposition.equals(Seperator[2])))

请注意||条件周围的额外括号。