If-Else Statement Error

2 ビュー (過去 30 日間)
Ghina Alhunaidi
Ghina Alhunaidi 2021 年 3 月 15 日
コメント済み: Ghina Alhunaidi 2021 年 3 月 15 日
code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
if code==25
fprintf ('Correct code! /n')
else
fprintf ('please try again /n')
code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
end
for some reason, i keep getting this error (Operator '==' is not supported for operands of type 'cell'. Error in EscapeRoom (line 2)
if code==25) whenever I input a value into the pop-up window and I've tried several functions and it still wont work, what do I have to change in my code to get it to work?

回答 (1 件)

the cyclist
the cyclist 2021 年 3 月 15 日
The inputdlg returns a cell array of the string that the user enters. You need to convert that to a numeric data type. Here is one way:
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = str2double(codeCell);
if code==25
fprintf ('Correct code! /n')
else
fprintf ('please try again /n')
code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
end
  5 件のコメント
the cyclist
the cyclist 2021 年 3 月 15 日
You are being sloppy in using the code I posted. You should spend more time trying to understand the functions I used, instead of just blindly copy/paste.
Specifically, you are mixing up how I used strcmp and how I used str2double. I would look only at the latest code I posted -- which did not use str2double at all -- and try again to make that work for your second code.
Ghina Alhunaidi
Ghina Alhunaidi 2021 年 3 月 15 日
I just figured it out and it works perfectly, thank you for the help.

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

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by