How to enter negative values ​​and values ​​greater than 9 in input with string.

4 次查看(过去 30 天)
clear
clc
n=10;
ii=1;
V=0;
display('number between 10 and 20!')
while ii<10
teste = input('Enter a value: ','s')
if strcmp(teste,'End')
display('You cannot type end before you have 10 values entered')
continue;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end
while (ii>10) | (ii<20)
teste = input('Enter a value: ','s')
if strcmp(teste,'End')
display('End Program')
break;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end

采纳的回答

Khalid Mahmood
Khalid Mahmood 2021-4-14
%for 10 values, while ii<11 is needed whe we start with ii=1, and
%string(teste) to convert character array into string
clear,clc
n=10;
ii=1;
V=0;
display('number between 10 and 20!')
while ii<11
teste = input('Enter a value: ','s')
if strcmpi(teste,'End')
display('You cannot type end before you have 10 values entered')
continue;
end
X(ii) = string(teste)
V(ii) = str2num(X(ii))
ii = ii+1;
end
fprintf('\n loop 2:');
while (ii>10) | (ii<20)
teste = input('Enter a value: ','s')
if strcmpi(teste,'End')
display('End Program')
break;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end
  3 个评论
Luccas S.
Luccas S. 2021-4-14
Okay, now it worked. I just had to reverse the order of the vectors X (ii) and V (ii).
X (ii) = str2double (test)
V (ii) = X (ii)
I didn't remember the strcmpi function, thanks.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by