how to write a program combininb name and age?

4 ビュー (過去 30 日間)
lior
lior 2024 年 8 月 13 日
コメント済み: Steven Lord 2024 年 8 月 13 日
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
  1 件のコメント
Steven Lord
Steven Lord 2024 年 8 月 13 日
Another approach could be to build up a string that includes your data. This documentation page shows two ways to do that.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by