Objective-C 字符串,字符串格式化程序
我有一个程序,dreamlax 和我一起做了很多工作,它使用 Objective-C 在华氏、摄氏度、开尔文和兰金温标之间转换温度,但将控制台输入转换为开尔文,然后从开尔文到最后 -用户所需的温标。
现在,我有一个想法,我想在计算完成后实现转换温度的最终提示。目前设置为仅显示如下:
x 度 温度标度
其中 x = 最终转换温度,温度标度 = 用户希望拥有的标度他的体温转换为。
假设最终用户选择华氏度作为他/她的源温度,希望将 212 度转换为他/她的目标摄氏温标。显然,转换应该等于 100 摄氏度,但我认为程序最好显示这样的结果:
212 度 华氏度 是 100摄氏度。
现在,我已经将需要用变量替换的值以粗体显示。我很容易解决了 212 和 100 值,因为 100 变量已经存在,并且可以通过将其替换为 sourceTemp 变量的字符串格式化程序(该变量包含用户原始输入的温度)来轻松补救。
现在,华氏字符串有点不同。
我试图在原始开关中建立一些新的东西,如下所示:
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
sourceTempText = 1;
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
sourceTempText = 2;
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
sourceTempText = 3;
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
sourceTempText = 4;
break;
}
好的,所以我添加了每个不同的情况,将名为 sourceTempText 的新变量设置为 1-4,与最终用户选择选择他/的值相同她的源头温度。
现在,这是我尝试使用最终开关向最终用户显示最终提示的方法:
switch (prompt2)
{
case 1:
//end-user chooses Fahrenheit
printf("%lf degrees sourceTempText is %lf degrees Fahrenheit\n", sourceTemp, [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("%lf degrees sourceTempText is %lf degrees Celsius\n", sourceTemp, [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("%lf degrees sourceTempText is %lf degrees Kelvin\n", sourceTemp, [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("%lf degrees sourceTempText is %lf degrees Rankine\n", sourceTemp, [converter rankineValue]);
break;
}
我现在不确定,是否可以像这里一样将 sourceTempText 插入到字符串中,也许我必须使用字符串格式化程序,但我不确定。这应该是一个简单的修复,我只是想把它扔在这里! :)
PS 对于这种混乱的问题格式感到抱歉,我有点不知道如何表达它,所以如果需要,请要求澄清。
I have a program with which dreamlax worked a lot with me on, which uses Objective-C to convert temperatures between the Fahrenheit, Celsius, Kelvin and Rankine temperature scales, but converting console-input into Kelvin, and then from Kelvin to the end-user's desired temperature scale.
Now, I have an idea I would like to implement for the final prompt of the converted temperatures once the calculations are done. It is set up currently to only display like so:
x degrees temperature-scale
Where x = the final converted temperature, and temperature-scale = the scale the user wishes to have his temperature converted to.
Suppose the end-user selected Fahrenheit as his/her source temperature, wishing to convert 212 degrees into his/her target temperature scale of Celsius. The conversions should equal 100 degrees Celsius obviously, but I think the program would be better of displaying the result like this:
212 degrees Fahrenheit is 100 degrees Celsius.
Now, I've made the values that need to be replaced by variables in bold. I have the 212 and 100 values solved easily, because 100 variable has been there in the first place, and 212 can easily be remedied by replacing it with the string formatter of the sourceTemp variable, the variable which contains the users original inputted temperature.
Now, the Fahrenheit string is a bit different.
I have tried to establish something new in the original switch like so:
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
sourceTempText = 1;
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
sourceTempText = 2;
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
sourceTempText = 3;
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
sourceTempText = 4;
break;
}
OK, so I have added to each different case, setting a new variable named sourceTempText to either 1-4, the same value that the end-user chose to pick his/her source temperature.
Now, here is how I tried to display the final prompt to the end-user with the final switch:
switch (prompt2)
{
case 1:
//end-user chooses Fahrenheit
printf("%lf degrees sourceTempText is %lf degrees Fahrenheit\n", sourceTemp, [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("%lf degrees sourceTempText is %lf degrees Celsius\n", sourceTemp, [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("%lf degrees sourceTempText is %lf degrees Kelvin\n", sourceTemp, [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("%lf degrees sourceTempText is %lf degrees Rankine\n", sourceTemp, [converter rankineValue]);
break;
}
I am not sure now, if I can insert sourceTempText into the string like I have here, instead, maybe I have to use a string formatter, but I'm not sure. It should be an easy fix, I just wanted to throw it out here! :)
P.S. sorry for the kind of messy question formatting, I kind of didn't know how to phrase it so please ask for clarification if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 sourceTempText 替换为 %s,并在
switch
语句上方声明可能的字符串,如下所示:然后在
sourceTemp、
之后向每个printf
添加参数> 就像这样:这是
prompt-1
因为计数从零开始,而你从 1 开始。ps. 我现在看到你设置了
sourceTempText = 1/2/ 3/4;
- 我刚刚使用了prompt
,因为它具有相同的值。您可以做的是设置和使用
scales[sourceTempText]
,同时确保在切换
之前,sourceTempText = 0
。这将成为输入清理的一个很好的例子:无论用户在提示符中输入什么内容,您的程序都将始终显示有效的文本。Replace sourceTempText with %s, and above the
switch
statement declare the possible strings like so:Then add and argument to each
printf
, after thesourceTemp,
like so:That's
prompt-1
because counting starts at zero, and you start at 1.ps. I see now you set
sourceTempText = 1/2/3/4;
- I just usedprompt
, since it has the same value. What you could do, is settingand using
scales[sourceTempText]
, while making sure that before theswitch
,sourceTempText = 0
. That would make a fine example of input sanitizing: whatever the user manages to put inprompt
, your program will always display a valid text.