gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:15:06 27 4
gpt4 key购买 nike

我想知道如何制作一些如果 X=5 且 Y=2 的东西,然后让它输出类似的东西Hello 2 World 5

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

那么我该如何在 TI-BASIC 中做到这一点?

最佳答案

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

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

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

:"?
: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,它解决了类似的问题。

关于ti-basic - 在 TI-BASIC 中,如何在字符串中间添加变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883743/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com