HashCode,尝试获取单词或数字的最后几个字母

发布于 2024-08-22 08:44:39 字数 681 浏览 1 评论 0原文

我有这个 hashCode 函数,我一直试图用 Eclipse 实现它,但我的测试一直失败。我正在尝试编写一个哈希码函数,该函数返回单词或数字的最后 2 或 3 个字符。这是我当前的代码:

 public int hashCode(String value){
 String test = value;
 int hash = 1;
 int len = test.length();
 System.out.println(len);
 for ( int i=0; i<len; i++ )
       {
       hash = 31 * hash + len;
       }
    return hash;
}

这是我的 Junit 测试:

    @Test // hashCode()
 public void testHashCode1() {
  //Integer key1 = 123;
  String key2 = "and";
  assertEquals("and", key2.hashCode());
 }

每次运行测试时,它都会失败,指示:

预期但为:<96727>

现在我真的想让这个函数接受一个单词或数字并返回所接受对象的最后两个或三个字符。有谁知道如何使用 hashCode 来做到这一点。任何帮助将不胜感激

I have this hashCode function that I have been trying to implement with Eclipse but my Tests keep failing. I am trying to write a Hashcode function that returns that last 2 or 3 chars of a word or number. Here is my current code:

 public int hashCode(String value){
 String test = value;
 int hash = 1;
 int len = test.length();
 System.out.println(len);
 for ( int i=0; i<len; i++ )
       {
       hash = 31 * hash + len;
       }
    return hash;
}

Here is my Junit Test:

    @Test // hashCode()
 public void testHashCode1() {
  //Integer key1 = 123;
  String key2 = "and";
  assertEquals("and", key2.hashCode());
 }

Everytime I run my test it fails indicating:

expected but was: <96727>

Now I really would like for this function to take in either a Word or Number and return the last two or three chars of the object taken in. Does anyone know how to do this using hashCode. Any help would be most appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-08-29 08:44:39

public int hashCode(String value)

并调用

assertEquals("and", key2.hashCode());

您甚至没有正确地覆盖它,请更改签名以不接受任何参数:)

public int hashCode(String value)

and calling as

assertEquals("and", key2.hashCode());

you are not even overriding it properly change the signature to take no arguments please :)

吖咩 2024-08-29 08:44:39

这可能是编辑器破坏代码的问题,但我没有看到循环内任何地方使用 valuetest

It may be a problem with the editor mangling the code, but I do not see value or test used anywhere inside the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文