日期:2014-05-20  浏览次数:20809 次

比较头疼的数组+循环+条件判断的问题
先看代码:
Java code
package User;
import java.util.Scanner;
public class UserManager 
{
    public static void main(String[] args) 
    {
        DbStore abc = new DbStore();
        
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = input.next();
        System.out.println("请输入密码:");
        int password = input.nextInt();
        for(int i=0;i<abc.getEntryArr().length;i++)    
        {
            if((!abc.getEntryArr()[i].getUserName().equals(name))&&(!abc.getEntryArr()[i].getUserPwd().equals(password)))
            {
                System.out.println("用户不存在或密码错误!");
            }
            else
            {
            System.out.println("该用户的真实姓名为"+abc.getEntryArr()[i].getRealName());
            }
        }
    }
}


下面是DbStore类:
Java code
package User;

public class DbStore 
{
    private UserEntity[] entryArr;//声明一个数组变量

    public DbStore() //初始化用户实体数组
    {
        UserEntity ue1 = new UserEntity();
        ue1.setAge(21);
        ue1.setCellPhone("888888881");
        ue1.setEmail("damo1@qq.com");
        ue1.setRealName("大漠1");
        ue1.setUserId("001");
        ue1.setUserName("damo1");
        ue1.setUserPwd("1");

        UserEntity ue2 = new UserEntity();
        ue2.setAge(22);
        ue2.setCellPhone("888888882");
        ue2.setEmail("damo2@qq.com");
        ue2.setRealName("大漠2");
        ue2.setUserId("002");
        ue2.setUserName("damo2");
        ue2.setUserPwd("2");

        UserEntity ue3 = new UserEntity();
        ue3.setAge(23);
        ue3.setCellPhone("888888883");
        ue3.setEmail("damo3@qq.com");
        ue3.setRealName("大漠3");
        ue3.setUserId("003");
        ue3.setUserName("damo3");
        ue3.setUserPwd("3");

        UserEntity ue4 = new UserEntity();
        ue4.setAge(24);
        ue4.setCellPhone("888888884");
        ue4.setEmail("damo4@qq.com");
        ue4.setRealName("大漠4");
        ue4.setUserId("004");
        ue4.setUserName("damo4");
        ue4.setUserPwd("4");

        UserEntity ue5 = new UserEntity();
        ue5.setAge(25);
        ue5.setCellPhone("888888885");
        ue5.setEmail("damo5@qq.com");
        ue5.setRealName("大漠5");
        ue5.setUserId("005");
        ue5.setUserName("damo5");
        ue5.setUserPwd("5");

        setEntryArr(new UserEntity[] { ue1, ue2, ue3, ue4, ue5 });
    }
    public static void main(String[] args) 
    {      
        DbStore uf = new DbStore();
        for(int i=0;i<uf.getEntryArr().length;i++)
        {
            System.out.println("用户编号:"+uf.getEntryArr()[i].getUserId());
            System.out.println("登陆名称:"+uf.getEntryArr()[i].getUserName());
            System.out.println("登陆密码:"+uf.getEntryArr()[i].getUserPwd());
            System.out.println("真实姓名:"+uf.getEntryArr()[i].getRealName());
            System.out.println("年龄:"+uf.getEntryArr()[i].getAge());
            System.out.println("手机号码:"+uf.getEntryArr()[i].getCellPhone());
            System.out.println("电子邮箱:"+uf.getEntryArr()[i].getEmail());
            System.out.println("\r\n");
            
        }
    }
    public void setEntryArr(UserEntity[] entryArr) {
        this.entryArr = entryArr;
    }
    public UserEntity[] getEntryArr() {
        return entryArr;
    }
}




执行的问题是:程序把输入的name和password 先与entryArr[0]比较,然后才是entryArr[1]/[2]/[3]/[4]