简体   繁体   English

如何测试列表中所有字符的长度,并根据长度调整Output()的位置?

[英]How can I test the length of all the characters in a list and adjust the location of Output( based on the length?

I wrote a program to roll stats for D&D 5e characters. 我编写了一个程序来统计D&D 5e角色的统计信息。 It rolls 4d6 and drops the lowest and adds that value to a list. 它滚动4d6并放下最低值,然后将该值添加到列表中。 It repeats this for a total of 6 times and then outputs the result to the screen. 总共重复6次,然后将结果输出到屏幕。 I want to be able to center the output so it looks nice and remove the {} brackets around the list as well as the commas between numbers. 我希望能够使输出居中,使其看起来不错,并删除列表中的{}括号以及数字之间的逗号。 The problem I'm running into with this is I can't just output a blank space to all the spots because everything after the second character is variable in what it is. 我遇到的问题是我不能只在所有位置输出空格,因为第二个字符之后的所有内容都是可变的。

Here's the code I'm using. 这是我正在使用的代码。

0→dim(∟STATS
For(I,1,6,1
0→dim(∟ROLLS
randInt(1,6,4)→∟ROLLS
1+sum(not(cumSum(∟ROLLS=min(∟ROLLS))))→X
∟ROLLS(X)-min(∟ROLLS)→∟ROLLS(X)
sum(∟ROLLS)→∟STATS(1+dim(∟STATS
End
ClrHome
While not(getKey
Output(5,4,∟STATS
End
ClrHome

An example output looks like this: 输出示例如下所示:

___{8,10,9,10,9,15}_______| 

Another one might look like this 另一个可能看起来像这样

___{13,9,14,11,9,10}______|

(| represents the end of the screen, _ represents spaces but shown for width reasons) (|代表屏幕的结尾,_代表空格,但出于宽度原因而显示)

Notice it always starts in the one spot but the rest os in varying locations. 请注意,它总是从一个位置开始,而其他位置则在不同位置。

In case it matters for the sake of screen size this is the TI-84 Plus CE 如果出于屏幕尺寸的考虑,这是TI-84 Plus CE

Strings are your friend! 弦乐是你的朋友!

I spoke with a friend on this and she gave me the idea to store the list elements into a string and then output the string instead of the list. 我和一位朋友谈过,她给我一个想法,将列表元素存储到字符串中,然后输出字符串而不是列表。 The trick was figuring out how to store the numbers in a string. 诀窍是弄清楚如何将数字存储在字符串中。 I found the toString( function which helped with that. 我找到了toString(函数对此有所帮助。

Here's the working code snippet. 这是工作代码段。

toString(∟STATS(1))→Str0
For(I,2,6,1
Str0+" "+toString(∟STATS(I))→Str0
End

That gave me the string of numbers and spaces formatted nicely. 那给了我一串数字和空格,格式很好。 My last issue was how to center it where again my friend came to my rescue and suggested getting the length of the string /2 as the column coordinate. 我的最后一个问题是如何将它居中,我的朋友再次来此求救,建议将字符串/ 2的长度作为列坐标。 I tried using that and was a bit off center so I subtracted 2 to get a column. 我尝试使用它,并且偏离了中心,所以我减去2得到一个列。 The way I did this is in the code below. 我这样做的方式在下面的代码中。

ClrHome
While not(getKey
Output(5,(int(length(Str0)/2-2)),Str0
End

So the full working code is 所以完整的工作代码是

0→dim(∟STATS
For(I,1,6,1
0→dim(∟ROLLS
randInt(1,6,4)→∟ROLLS
1+sum(not(cumSum(∟ROLLS=min(∟ROLLS))))→X
∟ROLLS(X)-min(∟ROLLS)→∟ROLLS(X)
sum(∟ROLLS)→∟STATS(1+dim(∟STATS
End
toString(∟STATS(1))→Str0
For(I,2,6,1
Str0+" "+toString(∟STATS(I))→Str0
End
ClrHome
While not(getKey
Output(5,(int(length(Str0)/2-2)),Str0
End
ClrHome
SetUpEditor

It's been ages since I used my TI but I think you'll have to iterate through the array and Output each number in turn. 自从我使用TI以来已经有很多年了,但是我认为您必须遍历数组并依次输出每个数字。 Position each according to the index and the size of the number. 根据索引和数字的大小定位每个位置。 Something like 就像是

For (X, 1, dim(STATS), 1)
Output (5,floor(1+3X+∟STATS(X)/2),∟STATS(X)
End

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

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