【问题标题】:Java Unscrabling character string even length works but odd length doesntJava Unscrambling 字符串偶数长度有效,但奇数长度无效
【发布时间】:2013-08-08 11:58:47
【问题描述】:

我正在编写一个具有多种编码方案的程序,其中一个是质数移位,基于键是偶数还是奇数。 解码方案如下::

The key is checked to see whether it is odd or even. For an odd key, odd numbered characters are in order first in the new string, then all even indexes. If the key is even all even indexes are first, then all the odd indexes

所以对于字符串“abcdefg”和 27 的键,新字符串应该是“bdfaceg” 如果密钥为 28,则新字符串应为“acegbdf”

奇怪的是 如果密钥是奇数,并且字符串长度是奇数或偶数,它会完美解码。 如果键是偶数并且字符串长度是偶数,它将很好地解码,

但是如果键是偶数并且字符串长度是奇数,它将无法正确解码。

使用测试字符串“在此处输入消息”。这些是我的输出::

Encoded Key = 28 ; Encoded Message "EtrMsaehr.ne esg ee" Message length = 19
Decoded Key = 28 ; Decoded Message "E.tnreM seasegh renull"

所以偶数条目在正确的位置,但奇数条目要么需要被反向拉动,要么需要被奇数索引推回,我认为...... 我认为将它们推回索引是最简单的,但我对 Java 还是很陌生,我不知道该怎么做。

这是我在这个实例中使用的函数的代码。

protected String decode(String a, int k)
{
    System.out.println(a.length());
    String[] out = new String [a.length()];
    String decode = a;
    int key = k;
    boolean kP = IsEven(key);
    String odd = "";

    if (kP)
    {
        //Key is even
        try
        {
            int f = 0;
            for (int i =0 ; i<(a.length()/2); i++)
            {
                out[f] = Character.toString(a.charAt(i));
                f+=2;
            }
            int g = 1;
            for (int i = (a.length()/2) ; i<(a.length()); i++)
            {
                out[g] = Character.toString(a.charAt(i));
                g+=2;
            }
        }
        catch ( IndexOutOfBoundsException e )
        {
            System.out.println("Out of bounds");
            while(true)
                break;
        }
    }
    else
    {
        //key is odd
        try
        {
            int f = 1;
            for (int i =0 ; i<(a.length()/2); i++)
            {
                    out[f] = Character.toString(a.charAt(i));
                    f+=2;
            }

            int g = 0;
            for (int i = (a.length()/2) ; i<(a.length()); i++)
            {
                    out[g] = Character.toString(a.charAt(i));
                    g+=2;
            }
        }
        catch ( IndexOutOfBoundsException e )
        {
            System.out.println("Out of bounds");
            while(true)
                break;
        }
    }
    for (int i = 0 ; i<a.length(); i++)
            odd += out[i];
        System.out.println(odd);
    return(odd);
}

【问题讨论】:

    标签: java string algorithm sorting decode


    【解决方案1】:

    您的问题是,当它是偶数键和奇数字符串时,编码字符串的第一个“序列”中会多一个字符,而您没有考虑到这一点:

    如果密钥为 28,则新字符串应为“acegbdf”

    上面的例子有 4 个字符,然后是 3 个字符。

    在您的代码中,您运行到(a.length()/2),在上面的字符串中是 3,这意味着您使用索引 0、1 和 2。当您真正想要的是使用 0、1、2 时, 和 3 ("aceg")。

    解决方案是在两个 for 循环中为偶数键添加 1...这也将解决您未能告诉我们的 OUTOFBOUNDSEXCEPTION!

    只是一个警告:我相信我的“解决方案”会导致你的偶数长度字符串失败,但我的工作不是做你的作业。 :)

    【讨论】:

    • 谢谢,我什至没有想到这一点。这也不是问题,它只是一个包罗万象的问题;如果它确实超出了界限,这意味着它已经完成了。它不应该发生,但它只是以防万一。我习惯于使用硬件,所以这是我的一种心态。
    【解决方案2】:

    对于一些简单的示例,甚至手动执行您的代码:

    • 密钥:2;字符串:“A”

    • 密钥:2;字符串:“AB”

    • 密钥:2;字符串:“ABC”

    详细检查会发生什么。当您知道发生了什么之后,您就可以纠正问题。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2016-01-10
      • 2011-02-24
      相关资源
      最近更新 更多