如何将这段Java代码转换为Rexx,以便生成与Luhn算法兼容的信用卡号?

huangapple go评论73阅读模式
英文:

How do I convert this Java code to Rexx inorder to generate Luhn compatible credit cards?

问题

public string GenerateCardTokenOptimised()
{
    int[] checkArray = new int[15];
    
    var cardNum = new int[16];

    for (int d = 14; d >= 0; d--)
    {
        cardNum[d] = _random.Next(0, 9);
        checkArray[d] = (cardNum[d] * (((d+1)%2)+1)) % 9;
    }

    cardNum[15] = (checkArray.Sum() * 9) % 10;

    var sb = new StringBuilder();

    for (int d = 0; d < 16; d++)
    {
        sb.Append(cardNum[d].ToString());
    }
    return sb.ToString();
}

在Rexx中似乎没有其他语言中常见的数组,所以我该如何实现这个功能?

英文:
       public string GenerateCardTokenOptimised()
        {
            int[] checkArray = new int[15];
            
            var cardNum = new int[16];
 
            for (int d = 14; d &gt;= 0; d--)
            {
                cardNum[d] = _random.Next(0, 9);
                checkArray[d] = ( cardNum[d] * (((d+1)%2)+1)) % 9;
            }
 
            cardNum[15] = ( checkArray.Sum() * 9 ) % 10;
 
            var sb = new StringBuilder(); 
 
            for (int d = 0; d &lt; 16; d++)
            {
                sb.Append(cardNum[d].ToString());
            } 
            return sb.ToString();
        }

It seems Rexx does not have the usual arrays in other languages so how can I implement this

答案1

得分: 2

Rexx有stem变量。Stem变量类似于Java中的Map<String, String>,用作数组的替代品。

你可以这样做

    do i = 1 to 10
       myArray.i = i
    end

你也可以在stem变量中使用字符串作为键

    color = &#39;green&#39;
    say &quot;Color value: &quot; colorLookup.color

你还可以检查是否找到了某个值

   false = 0
   true = 1

   found. = false /* 将stem初始化为false (0) */

   do i = 1 to 10
      square = i * i
      found.square = true
   end

   do i = 1 to 100
      if found.i = true then do
         say i &quot;是一个平方数&quot;
      end
   end


<details>
<summary>英文:</summary>

Rexx has [stem](https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.ikja300/stems.htm) variables. Stem variables are **Associate arrays** like **Map&lt;String, String&gt;** in Java and are used instead of arrays.

You can do 

do i = 1 to 10
   myArray.i = i
end

you can also use strings as key in stem variables

color = &#39;green&#39;
say &quot;Color value: &quot; colorLookup.color

You can also check if something is found

false = 0
true = 1

found. = false /* initialize stem to false (0) */

do i = 1 to 10
square = i * i
found.square = true
end

do i = 1 to 100
if found.i = true then do
say i "is a square"
end
end

huangapple
  • 本文由 发表于 2020年9月26日 01:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定