how to write a program combininb name and age?

4 次查看(过去 30 天)
In my HB I got an assigment on writing a program that asks for the name and age of a person, then runs the sentence Dear__your age is___, the blank spaces containing the input
I thought about using a switch, the cases being the name inputs, is that something I can do or is there an easier way?

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2024-8-13
移动:Fangjun Jiang 2024-8-13
No Switch statement is needed. This practice is to get the input and put it in the right format.
help input
INPUT Prompt for user input. RESULT = INPUT(PROMPT) displays the PROMPT string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the value in RESULT. To evaluate expressions, INPUT accesses variables in the current workspace. If you press the return key without entering anything, INPUT returns an empty matrix. STR = INPUT(PROMPT,'s') returns the entered text without evaluating expressions. To create a prompt that spans several lines, use '\n' to indicate each new line. To include a backslash ('\') in the prompt, use '\\'. Example: reply = input('Do you want more? Y/N [Y]:','s'); if isempty(reply) reply = 'Y'; end See also KEYBOARD. Documentation for input doc input
help sprintf
sprintf - Format data into string or character vector This MATLAB function formats the data in arrays A1,...,An using the formatting operators specified by formatSpec and returns the resulting text in str. Syntax str = sprintf(formatSpec,A1,...,An) [str,errmsg] = sprintf(formatSpec,A1,...,An) str = sprintf(literalText) Input Arguments formatSpec - Format of output fields formatting operators A1,...,An - Numeric, character, or string arrays arrays literalText - Input text without formatting operators character vector | string scalar Output Arguments str - Formatted text character vector | string scalar errmsg - Error message character vector Examples openExample('matlab/FloatingPointFormatsExample') openExample('matlab/LiteralTextandArrayInputsExample') openExample('matlab/SpecifyFormattedTextAsStringArrayExample') openExample('matlab/IntegerFormatwithFloatingPointInputsExample') openExample('matlab/SpecifyFieldWidthofaPrintedValueExample') openExample('matlab/ReorderInputsUsingPositionIdentifiernExample') openExample('matlab/CreateaStringfromValuesinCellArrayExample') See also char, fprintf, int2str, num2str, sscanf, fscanf, compose, string Introduced in MATLAB before R2006a Documentation for sprintf doc sprintf Other uses of sprintf codistributed/sprintf dlarray/sprintf gpuArray/sprintf matlab.automation.internal.diagnostics.FormattableString/sprintf

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by