Does anyone know why my 2nd conditional statement is being ignored in this while loop?

1 回表示 (過去 30 日間)
Does anyone know why my second conditional statement "units(1) ~= 'n'" is being ignored when this code block is run? When the 2nd statement is removed the code runs without problems.
units = strings([1,4]);
torque_force_input = input('Input the unit of force for your initial torque\n','s');
units(1)= torque_force_input;
while units(1) ~= 'N'||units(1) ~= 'n'
disp('Please input a valid unit for the torque''s force.');
torque_force_input = input('Input a unit of force for your torque...\n','s');
units(1)= torque_force_input;
end
  1 件のコメント
Stephen23
Stephen23 2020 年 5 月 1 日
Given A~=B, can you think of a single value of X which will make this statement false?:
X~=A || X~=B

サインインしてコメントする。

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 5 月 1 日
編集済み: Mehmed Saad 2020 年 5 月 1 日
Suppose i enter Nm in command window then units will be
units
units =
1×4 string array
"Nm" "" "" ""
if you try the access the first letter
units(1)
ans =
"Nm"
you have the complete word the user enters and not a single character
to access first index
units{1}(1)
ans =
'N'
Now for the while loop, it keeps runing untill it receives false. so if user inputs N, so
'N' ~= 'N' || 'N' ~= 'n'
this will give you true and while loop will keep runing. you need a condition such that if any of them is true you get false and if both are false you get true

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by