| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 352 人关注过本帖, 1 人收藏
标题:h.contains(id) 怎么没有用啊 哪位大神帮忙看看
只看楼主 加入收藏
zhouwei025
Rank: 1
来 自:diqiu
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-7-29
结帖率:100%
收藏(1)
 问题点数:0 回复次数:2 
h.contains(id) 怎么没有用啊 哪位大神帮忙看看
package work1;
import java.util.*;
public class TestEmployee {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        HashSet h=new HashSet();
        do {
            System.out.println("请选择:1.添加员工  2.显示信息  3.按姓名查询");
            int select = sc.nextInt();
            if (select == 1) {
                System.out.println("请输入姓名:");
                String name = sc.next();
                System.out.println("请输入工资金额:");
                double salary = sc.nextDouble();
                String id;
                do {
                    System.out.println("请输入员工号:");
                    id = sc.next();
                    if(h.contains(id)){   //******************
                        break;
                    } else {
                        System.out.println("此员工号已存在,请重新输入!");
                    }
                } while (true);
                Employee emp = new Employee(name, id, salary);
                h.add(emp);
            } else if (select == 2) {
                Iterator it=h.iterator();
                while(it.hasNext()){
                    System.out.println(it.next().toString());
                }
            } else if (select == 3) {
               
            } else if (select == 0) {
                break;
            } else {

            }
        } while (true);
        
    }
}
//*******************************************************
public class Employee {
    private String name;
    private String id;
    private double salary;
    public Employee(String name, String id, double salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "姓名:" + name+"  工号:" + id  + "  工资:" + salary;
    }   
}
2015-09-23 23:44
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
收藏
得分:0 
h里面放的是Employee对象,不是String,类型不匹配
h.contains(id)这样每次都是false

if(h.contains(id)){   //******************
    break;
} else {
    System.out.println("此员工号已存在,请重新输入!");
}
还有这块写反了,在包含的时候应该打印提示信息,否则跳出
2015-09-24 08:40
zhouwei025
Rank: 1
来 自:diqiu
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-7-29
收藏
得分:0 
恩恩  谢谢
2015-09-24 20:24
快速回复:h.contains(id) 怎么没有用啊 哪位大神帮忙看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015022 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved