有没有办法让变量值等于文本?尝试在 fprintf 行的中间显示用户选择的词

Is there a way to have a variable value equal to text? Trying to display a user-chosen word in the middle of an fprintf line

我正在尝试在 Mac 上用 Octave 编写 MadLibs 类型的游戏,但不知道如何在文本字符串中间显示用户选择的 WORD。我使用 fprintf 来显示中间带有变量的句子,只要变量等于 NUMBER,它就可以正常工作。如果您输入数字以外的任何内容,则会出现错误。

    Word=input('Enter a word:            ');
    fprintf('You chose %d as the word.\n', Word)

我如何允许用户选择一个词,然后将该词显示在一串文本的中间?

提前致谢。

stringsformat specifier%s 因此您可以将代码段更改为:

fprintf('You chose %s as the word.\n', Word)

Input 可以选择将输入的文本作为 MATLAB 字符串返回,而不计算表达式。

STR = input(PROMPT,'s')

这样无论输入什么都将作为字符串输出。 例如:

Word=input('Enter a word:            ','s');
fprintf('You chose %d as the word.\n', Word)

数字或文字将正确显示。