繁体   English   中英

字符串的Java模数算法

[英]Java modulus arithmetic for string

我正在为我的大学项目创建一个简单的Java程序,但无法创建数组字符串模数。 该程序只获取一个字符串,并针对数组中的字母从+5或-5对其进行加密或解密。 但是,如果我在输入字符串中输入数组(5,6,7,8,9)的最后5个中的任何一个,它将不返回任何内容,但应在开始时返回(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”的结果。

如果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追加到末尾,然后在StringBuilder上调用toString()

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

让我们开始第一个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 +数组[diff-1]

n-5取小牛肉时应执行的类似检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM