how to write a program combininb name and age?

조회 수: 5 (최근 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 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