필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Can someone tell me how can I create a if statement with mulitple else?

조회 수: 1 (최근 30일)
Minhao Wang
Minhao Wang 2020년 9월 24일
마감: MATLAB Answer Bot 2021년 8월 20일
clc;clear
C1 = input("Please enter a code to break: " , "s");
if length(C1) ~= 6
disp("Decoy message: Not a six-digit number.");
else
A = sum(C1 - '0');
if
mod(A , 2) = 1;
disp("Decoy message:Sum is odd.");
else
C2 = (C1(1) - '0') * (C1(2) - '0') - (C1(3) - '0');
switch C2
case 1
disp("Monday")
case 2
disp("Tuesday")
case 3
disp("Wednesday")
case 4
disp("Thursday")
case 5
disp("Friday")
case 6
disp("Saturday")
case 7
disp("Sunday")
otherwise
disp("Decoy message:Invalid rescue day.");
end
How do I use If else statement correctly?

답변 (1개)

Walter Roberson
Walter Roberson 2020년 9월 24일
if C2 == 1
disp('Monday');
elseif C2 == 2
disp('Tuesday');
else
disp('something else');
end
  댓글 수: 2
Minhao Wang
Minhao Wang 2020년 9월 24일
My homework does not let me to use if elseif for the part C2, only switch statement is allowed... Can you please show me how to do this with switch statement?
Walter Roberson
Walter Roberson 2020년 9월 25일
?? You already seem to be using switch(), so I am not clear as to what you want to do?
Perhaps you are looking for
if mod(A , 2) == 1;
disp("Decoy message:Sum is odd.");
elseif some condition
your switch code
else
some more code for when the sum is odd but the switch does not apply
end

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by