字符串的Java模算术

时间:2011-12-11 11:05:30

标签: java

我正在为我的大学项目创建一个简单的Java程序,但我无法制作数组字符串模数。该程序只需一个字符串,并从+5或-5加密或解密它,用于数组中的字母。但是如果我在输入字符串中输入数组中的最后5个中的任何一个(5,6,7,8,9)它返回什么,但它应该返回(a,b,c,d,e)开头的字母数组。

我必须使用的程序是非常原始的,只允许代码以某种方式,因此它的编码就像这样。

int i;
int n;
int x;
int inputValue;
int intArrayLength;
int intInputLength;
String array;
String inputString;
String newInputString;
String outputString;

System.out.print("Would you like to: 1 = Encrypt - 2 = Decrypt");
System.in.read(inputValue);
System.out.println("Would you like to: 1 = Encrypt - 2 = Decrypt [ " + inputValue + " ]");
System.out.print("Please enter string:");
System.in.read(inputString);
System.out.println("Please enter string: [ " + inputString + " ]");

array = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";
intArrayLength = length(array);
intInputLength = length(inputString);

if (intInputLength != 0)
   {
   if (inputValue == 1)
      {



      System.out.println("Please wait encrypting...");

      newInputString = "";
      for (i=0; i<=intInputLength; i++)
         {
         for (n=0; n<=intArrayLength; n++)
            {
            if (inputString[i] == array[n])
               {
               x = (n+5);
               if (x > intArrayLength)
                  {
                  x = x - intArrayLength;
                  newInputString = newInputString + array[x];
                  }
               else
                  {
                  newInputString = newInputString + array[x];
                  }
               }
            }
         }

      System.out.print("Would you like to reverse the string? 1 = Yes - 2 = No");
      System.in.read(inputValue);
      System.out.println("Would you like to reverse the string? 1 = Yes - 2 = No [ " + inputValue + " ]");

      if (inputValue == 1)
         {
         outputString = newInputString;
         newInputString = "";
         for (i=intInputLength-1; i>=0; i--)
            {
            newInputString = newInputString + outputString[i];
            }

         outputString = newInputString;
         System.out.print("Encrypted String: [ " + outputString + " ]");
         System.out.println();

         }
      else
         {
         outputString = newInputString;
         System.out.print("Encrypted String: [ " + outputString + " ]");
         System.out.println();
         }

      inputString = "";
      newInputString = "";



      }
   else
      {



      System.out.print("Has the string been reversed? 1 = Yes - 2 = No");
      System.in.read(inputValue);
      System.out.println("Has the string been reversed? 1 = Yes - 2 = No [ " + inputValue + " ]");

      if (inputValue == 1)
         {

         newInputString = "";
         for (i=intInputLength-1; i>=0; i--)
            {
            newInputString = newInputString + inputString[i];
            }

         inputString = newInputString;
         }

      System.out.println("Please wait decrypting...");

      newInputString = "";
      for (i=0; i<=intInputLength; i++)
         {
         for (n=0; n<=intArrayLength; n++)
            {
            if (inputString[i] == array[n])
               {
               x = (n-5);
               if (x <= 0)
                  {
                  x = x + intArrayLength;
                  newInputString = newInputString + array[x];
                  }
               else
                  {
                  newInputString = newInputString + array[x];
                  }
               }
            }
         }

      outputString = newInputString;
      System.out.print("Decrypted String: [ " + outputString + " ]");
      System.out.println();

      inputString = "";
      newInputString = "";



      }
   }

我已经编辑了上面的代码。在改变“Azodious”建议之后我现在得到的结果。

当我加密“cat999”时,我得到“hfyeeef”的结果“f”来自何处? 当我解密“hfyeeef”时,我得到“ct995”的结果,“a”发生了什么,“5”是加密中附加“f”的结果。

2 个答案:

答案 0 :(得分:2)

如果n + 5 is >= array.length,则只需减去array.length

int indexOfLetterToAppend = n + 5;
if (indexOfLetterToAppend >= array.length) {
    indexOfLetterToAppend -= array.length;
}

同样,如果n - 5 < 0,则只需添加array.length

int indexOfLetterToAppend = n - 5;
if (indexOfLetterToAppend < 0) {
    indexOfLetterToAppend += array.length;
}

请注意,在循环中连接到String效率非常低。它会创建临时StringStringBuilder实例的大量内容。使用StringBuilder追加到最后,然后在toString()上致电StringBuilder

另请注意,如果array[index]array,则无法使用String。您必须使用array.charAt(index),或将String转换为char数组。

答案 1 :(得分:1)

让我们开始你的第一个for循环:

array = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";   

newInputString = ""; 

for (i=0; i<=intInputLength; i++)          
{          \
    for (n=0; n<=intArrayLength; n++)             
    {             
        if (inputString[i] == array[n])                
        {                
          newInputString = newInputString + array[(n+5)];                
        }
    } 
}

如果你给inputString = "56789";在n == 32 array[(n+5)]变为array[37]。 (在条件范围内)

索引37肯定比array.length更多,你得到空字符串。

所以,当你发现(n+5) > array.length计算如下:

  1. int diff =(n + 5) - array.length
  2. newInputString = newInputString + array [diff-1]
  3. n-5获得价值时应执行的类似检查。