How can I make this program accept input 'value' as only a positive number except for the case 1(where i would like to have both negative and positive numbers as input)?
13 次查看(过去 30 天)
显示 更早的评论
The input 'value' only takes integers to proceed(it keeps asking for input until the user inputs only a number) but i want the the input 'value' to accept only positive numbers for all cases under the switch y except for the case 1(i.e. I want case 1 to accept both positive and negative numbers)
a = input('Enter the num of your choice between 1 to 4 ');
while true
value = input('Enter any value that you want to convert:','s');
disp(' ')
if ~isnan(str2double(value))
value=str2double(value);
break;
else
disp(' ')
disp('Wrong type of value entered. Try again.');
disp('HINT:Make sure to Enter input in numbers.')
disp(' ')
end
end
switch a
case 1
R=value*5
case 2
R= value * 6
case 3
R = value*7
case 4
R = value*8
otherwise
end
回答(1 个)
Are Mjaavatten
2018-4-16
if a == 1 R = value*5; else if value<0 disp('Value must be positive') else R = value*(a+4); end end disp(R)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!