check for integers and positive numbers

14 次查看(过去 30 天)
dat
dat 2023-11-19
编辑: VBBV 2023-11-19
clc, clear
[a, b, c] = LastName_FirstName_ME_Q_7_FN_input();
[A, B, C] = LastName_FirstName_ME_Q_7_FN_calc(a, b, c);
LastName_FirstName_ME_Q_7_FN_display(a, b, c, A, B, C);
function [a, b, c] = LastName_FirstName_ME_Q_7_FN_input() % function definition
a = input('Side a: '); % input side a
while floor(a) ~= a && a > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
a = input('Side a: ');
end
b = input('Side b: '); % input side b
while floor(b) ~= b && b > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
b = input('Side b: ');
end
c = input('Side c: '); % input side c
while floor(c) ~= c && c > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
c = input('Side c: ');
end
end
function [A, B, C] = LastName_FirstName_ME_Q_7_FN_calc(a, b, c) % function definition
A = rad2deg(acos((b^2 + c^2 - a^2)/(2*b*c))); % calculate angle A
B = rad2deg(acos((a^2 + c^2 - b^2)/(2*a*c))); % calculate angle B
C = rad2deg(acos((a^2 + b^2 - c^2)/(2*a*b))); % calculate angle C
if (A+B+C) > 180 && (A+B+C) < 180 % check if sum of angles is 180 degrees or not
A = 0;
B = 0;
C = 0;
end
end
function LastName_FirstName_ME_Q_7_FN_display(a, b, c, A, B, C) % function definition
if A == 0 || B == 0 || C == 0 % if any of the angle is 0 then the traingle is not possible
fprintf('a : %d\nb : %d\nc : %d\n', a, b, c);
fprintf('The traingle is not defined!\n');
else % display the side and the respective angles
fprintf('a : %d\tA : %f\n', a, A);
fprintf('b : %d\tB : %f\n', b, B);
fprintf('c : %d\tC : %f\n', c, C);
end
end
*I don't get why it still accepts negative numbers

回答(1 个)

Walter Roberson
Walter Roberson 2023-11-19
while floor(a) ~= a && a > 0 %
Consider some examples:
Suppose a = -2.5, then floor(-2.5) ~= -2.5 would be true so the first part of the test would succeed. But -2.5 > 0 would fail, so the re-prompt would not be done.
Suppose a = -2, then floor(-2) ~= -2 would be false so the first part of the test would fail, and when the first part fails && does not even try the second part, so the re-prompt would not be done
Suppose a = +2 then floor(2) ~= 2 would be false so the first part of the test would fail, and when the first part false && does not even try the second part, so the re-prompt would not be done
Suppose a = +2.5 then floor(2.5) ~= 2.5 would be true so the first part of th test would succeed. Then 2.5 > 0 is true so the second part would succeed as well. In this case, the reprompt would be done.
You need to think more about the difference between "and" and "or". And when you pick out different code, mentally run through the possibilities like I did here, to be sure that you got it right.
  1 个评论
VBBV
VBBV 2023-11-19
编辑:VBBV 2023-11-19
When you define the input values outside of the function calls, then it will ask only once whenver the user enters the negative numbers. However, when the user inputs positive numbers satisfying the while loop condition then it iterates untill the condition is not satisfied
clc, clear
% define the input sections outside of the function calls
a = input('Side a: '); % input side a
b = input('Side b: '); % input side a
c = input('Side c: '); % input side a
if a < 0 | b < 0 | c < 0
disp('enter a positive number');
return;
end
[a, b, c] = LastName_FirstName_ME_Q_7_FN_input(a,b,c);
[A, B, C] = LastName_FirstName_ME_Q_7_FN_calc(a, b, c);
LastName_FirstName_ME_Q_7_FN_display(a, b, c, A, B, C);
function [a, b, c] = LastName_FirstName_ME_Q_7_FN_input(a,b,c) % function definition
while floor(a) ~= a && a > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
a = input('Side a: ');
end
while floor(b) ~= b && b > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
b = input('Side b: ');
end
while floor(c) ~= c && c > 0 % check if integer or not and greater than 0 (doesn't allow float or double)
c = input('Side c: ');
end
end
function [A, B, C] = LastName_FirstName_ME_Q_7_FN_calc(a, b, c) % function definition
A = rad2deg(acos((b^2 + c^2 - a^2)/(2*b*c))); % calculate angle A
B = rad2deg(acos((a^2 + c^2 - b^2)/(2*a*c))); % calculate angle B
C = rad2deg(acos((a^2 + b^2 - c^2)/(2*a*b))); % calculate angle C
if (A+B+C) > 180 && (A+B+C) < 180 % check if sum of angles is 180 degrees or not
A = 0;
B = 0;
C = 0;
end
end
function LastName_FirstName_ME_Q_7_FN_display(a, b, c, A, B, C) % function definition
if A == 0 || B == 0 || C == 0 % if any of the angle is 0 then the traingle is not possible
fprintf('a : %d\nb : %d\nc : %d\n', a, b, c);
fprintf('The traingle is not defined!\n');
else % display the side and the respective angles
fprintf('a : %d\tA : %f\n', a, A);
fprintf('b : %d\tB : %f\n', b, B);
fprintf('c : %d\tC : %f\n', c, C);
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by