如何在MATLAB中测试整数? [英] How do I test for integers in MATLAB?

查看:155
本文介绍了如何在MATLAB中测试整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个计算整数阶乘的程序。但是,我坚持的部分是如果有人输入非整数,如 1.3 ,我希望能够测试输入并显示您输入的数字不是整数

I'm writing a program that will calculate factorials of integers. However, the part I'm stuck on is if someone enters a non-integer such as 1.3, I'd like to be able to test the input and display "The number you have entered is not an integer"

推荐答案

您可以使用 mod 功能,返回除法后的余数。所有整数都可以被 1 整除。所以对非整数的一个很好的测试是

You can use the mod function, which returns the remainder after division. All integers are divisible by 1. So a good test for non-integer would be

integerTest=~mod(value,1);

如果<$ c $,则返回 0 c> value 不是整数,如果是,则 1 。然后,您可以将此作为条件来拒绝非整数用户输入。

This returns 0 if value is not an integer and 1 if it is. You can then use this as a conditional to reject non-integer user inputs.

这篇关于如何在MATLAB中测试整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆