我真的对此很陌生。这属于我的else语句,但我想将其重定向到开头。我在想''
false = input('You have entered an invalid number .Please try again and enter correct the unit (1-3) you wish to convert: ');
while ~x ==1 || x== 2|| x||3
x = input('Enter the unit (1-3) you wish to convert: ');
end
''
如果检查错误的输入,则条件语句需要&&
,而不是||
。这应该行得通。
isInputWrong = true;
while (isInputWrong)
x = input('Enter 1, 2, or 3: ');
isInputWrong = (x ~= 1 && x~=2 && x~=3);
if (isInputWrong)
disp('Your input is wrong');
end
end
我发现了:
while x<1 || x>3
x = input('You have entered an invalid number. Please Enter the unit (1-3) you wish to convert: ');
end
我喜欢any
:
while ~any(x, [1 2 3]) % Read: "while x doesn't match any in [1 2 3]"
x = input('You have entered an invalid number. Please enter the unit (1-3) you wish to convert: ');
end