Effacer les filtres
Effacer les filtres

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

5 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Khalid Mahmood
Khalid Mahmood le 14 Avr 2021
%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 commentaires
Adam Danz
Adam Danz le 14 Avr 2021
It's also better to use str2double than str2um. See discussion.
Luccas S.
Luccas S. le 14 Avr 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Extend Testing Frameworks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by