【发布时间】:2013-12-21 14:25:15
【问题描述】:
所以我对 Hashmaps 和登录功能有疑问。
使用 addLogin 时,我需要输入参数,但这样做没有意义,因为我已经在构造函数类中完成了此操作。我将如何简单地使用 addLogin 并将姓氏、名字和个人 ID 号添加到哈希图中?
使用 Math.round(Math.random()*999+1) 生成 1-999 之间的随机数后,我应该如何将其与其他学生详细信息一起添加到哈希图中?
这是适用于这两个问题的完整代码,对于我对 Java 非常陌生的愚蠢问题表示歉意!我非常感谢我收到的任何帮助。提前致谢。
public class TestApplication
{
// hashmap
private HashMap<String, ArrayList <String>> Application = new HashMap<String, ArrayList <String>>();
// hashset
private HashSet<String> loginsIssued = new HashSet<String>();
// An Arry List for storing student information
private ArrayList<String> Student = new ArrayList<String>();
/**
* Constructor for objects of class Application
*/
public TestApplication(String Surname, String personalIdNo)
{
if (isValidpersonalIdNo(personalIdNo) == true)
{
Student.add(Surname);
Application.put(personalIdNo, Student);
System.out.println("Application number ### " + "has registered successfully");
}
else
{
System.out.println("Application has failed, Personal id: " + personalIdNo);
}
}
/**
* Create a Student Information
*/
public void TestApplication(String personalIdNo, String Surname, String Forename)
{
Student.add(Surname);
Student.add(Forename);
Student.add (personalIdNo);
}
/**
* Add Login
* Pull First Letter of Forenames
* Pull First Letter of Surname
* Generate Random Number
* Print
*/
public void addLogin(String Surname, String Forename)
{
String login = "";
{
System.out.println (Surname.charAt(0) + "" + " " + Forename.charAt(0) + " " + Math.round(Math.random()*999+1));
Student.add(login);
loginsIssued.add(login);
}
}
/**
* CONDITION 1
* Check whether the ID supplied is only numbers
*/
public boolean isNumeric(String personalIdNo)
{
if (personalIdNo.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
return true;
}
else
{
return false;
}
}
/**
* CONDITION 2
* Check whether the ID supplied has a length of 10
*/
public boolean checkLength(String personalIdNo)
{
if (String.valueOf(personalIdNo).length()==10)
{
return true;
}
else
{
return false;
}
}
/**
* CONDITION 3
* Check whether the ID supplied starts with 1
*/
public boolean checkFirstDigit(String personalIdNo)
{
if (personalIdNo.startsWith("1"))
{
return true;
}
else
{
return false;
}
}
/**
* Validation Check - Check if it satisfies all conditions.
*/
public boolean isValidpersonalIdNo(String personalIdNo)
{
if (isNumeric(personalIdNo) && checkLength(personalIdNo) && checkFirstDigit(personalIdNo))
{
return true;
}
else
{
return false;
}
}
/**
* FORENAME
* Add Forename
*/
public void addForename(String Forename)
{
Student.add(Forename);
}
/**
* Return Surname
*/
public String getSurname()
{
return Student.get(0);
}
}
【问题讨论】:
-
与您的问题无关,但给方法取同名而不是给类取同名是个坏主意。(
public void TestApplication)。你可以认为它是一个构造函数。 -
对于 1,您必须输入哪些您认为不必要的参数?
-
你到底想达到什么目的?你的程序到底应该做什么?除了关于 1 的问题,我还有关于 2 的问题:你打算如何处理你生成的号码?你只是暂时显示它。你想用它来创建个人ID吗?
-
基本上 addLogin 不带参数,取每个名字的第一个字符和 student 中包含的姓氏的第一个字符,并添加一个 1 到 999 范围内的随机数以形成一个登录名,然后添加为学生收藏的最终条目。记录保存在进一步集合中创建的所有此类登录,可从任何应用程序对象访问,称为 loginsIssued。然后使用它来确保分配的每个登录名都是唯一的。
-
关于您的代码的另一个说明:您的变量和方法名称应始终以小写字母开头。这并不重要,但它使事情变得更加清晰。此外,我不会使用 ArrayList 来表示学生,而是使用姓氏、名字...作为字段来创建特定的班级。