在 TI-BASIC 中,如何在字符串中间添加变量?

In TI-BASIC, how do I add a variable in the middle of a String?

我想知道如何在 X=5 和 Y=2 的情况下制作一些东西,然后让它输出类似 Hello 2 World 5.

在Java我会做 String a = "Hello " + Y + " World " + X; System.out.println(a);

那么在 TI-BASIC 中我该怎么做呢?

您有两个问题需要解决,连接字符串和将整数转换为字符串表示形式。

字符串连接非常简单,并使用 + 运算符。在你的例子中: "Hello " + "World" 将产生字符串“Hello World”。

在 TI-BASIC 中将数字转换为字符串并不容易,但可以使用与 TI-83+/84+ 系列兼容的方法 here。以下代码和解释引用自链接页面:

:"?
:For(X,1,1+log(N
:sub("0123456789",ipart(10fpart(N10^(-X)))+1,1)+Ans
:End
:sub(Ans,1,length(Ans)-1?Str1

With our number stored in N, we loop through each digit of N and store the numeric character to our string that is at the matching position in our substring. You access the individual digit in the number by using iPart(10fPart(A/10^(X, and then locate where it is in the string "0123456789". The reason you need to add 1 is so that it works with the 0 digit.

In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used for. Each time through the For( loop, we concatenate the string from before (which is still stored in the Ans variable) to the next numeric character that is found in N. Using Ans allows us to not have to use another string variable, since Ans can act like a string and it gets updated accordingly, and Ans is also faster than a string variable.

By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy character to the string initially, we now need to remove it, which we do by getting the substring from the first character to the second to last character of the string. Finally, we store the string to a more permanent variable (in this case, Str1) for future use.

转换为字符串后,您只需使用 + 运算符即可将字符串文字与转换后的数字字符串连接起来。

您还应该看看 a similar Stack Overflow question,它解决了类似的问题。

如果您知道 "Hello" 和 "World," 的长度,那么您可以简单地使用 Output() 因为 Disp 在每个语句后创建一个新行。

这个问题可以使用5.2.0版本引入的toString(函数,这个函数将数字转换成字符串,可以很方便的把数字和字符串显示在一起,结果会是这样:

Disp "Hello "+toString(Y)+" World "+toString(X)