How to i make it display correct for the right answer?

조회 수: 7 (최근 30일)
Kalpha.mc
Kalpha.mc 2020년 11월 11일
답변: Sourabh Kondapaka 2020년 11월 16일
diagonal = true;
for index = 1:1
disp(' straight , sideways , diagonal')
x = input(' Which way does checker pieces move? ','s');
if x ~= diagonal
disp('False, Start Over!')
return
elseif x == diagonal
disp('Correct!')
break
end
end
%% It should display correct when put in diagonal???

답변 (1개)

Sourabh Kondapaka
Sourabh Kondapaka 2020년 11월 16일
In the first line of your code snippet, you are defining a boolean variable 'diagonal' and set it to true.
But in the if/else conditional you are checking if the value of the variable x is diagonal or not.
For comparing strings you can use the strcmp() function.
Below is the working code of what you intended to achieve.
for index = 1:1
disp('straight , sideways , diagonal')
x = input('Which way does checker pieces move? ','s');
if strcmp(x,'diagonal')
disp('Correct!');
break
else
disp('False, Start Over!');
return;
end
end
For more information on strcmp() function, please refer this link
I would recommend the free Matlab Onramp Course to help you ramp up with the fundamentals of MATLAB Programming Language.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by