英文:
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 >= 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();
}
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 = 'green'
say "Color value: " 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 "是一个平方数"
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<String, String>** 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 = 'green'
say "Color value: " 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论