MATLAB Answers

Working with while loops.

1 ビュー (過去 30 日間)
Chase Viche
Chase Viche 2018 年 11 月 15 日
コメント済み: Chase Viche 2018 年 11 月 16 日
I am having problems setting this up "In the main script file, prompt the user for ? through appropriate use of an input command (doc input) and while loop (doc while) to ensure that ? is an integer value (doc rem) and greater than or equal to 1. Also, prompt the user for ? through appropriate use of an input command and while loop to ensure that ? is a real value (doc isreal) and is greater than or equal to 0.5." This is what I have but it isn't working correctly.
clear
clc
n = input(['Enter value for n >> ']);
while ((n >= 1) && (rem(n,1)~=0))
disp(['The answer must be an integer and greater than or equal to 1'])
n = input('Enter value for n >> ');
end
x = input(['Enter value for x >> ']);
while ((isreal(x)) && (x >= 0.5))
disp(['The answer must be real and greater than or equal to 0.5'])
x = input(['Enter value for x >> ']);
end

  0 件のコメント

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

回答 (1 件)

KSSV
KSSV 2018 年 11 月 16 日
n = input('Enter value for n >> ');
while ~((n >= 1) && (rem(n,1)~=0))
disp('The answer must be an integer and greater than or equal to 1')
n = input('Enter value for n >> ');
end
x = input('Enter value for x >> ');
while ~((isreal(x)) && (x >= 0.5))
disp('The answer must be real and greater than or equal to 0.5')
x = input('Enter value for x >> ');
end

  1 件のコメント

Chase Viche
Chase Viche 2018 年 11 月 16 日
This all works except the (rem(n,1)~=0) part of the code.

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

Community Treasure Hunt

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

Start Hunting!

Translated by