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)?

8 ビュー (過去 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
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)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by