如果数字以1结尾做某事 [英] If number ends with 1 do something

查看:135
本文介绍了如果数字以1结尾做某事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

if(day==1 || day==11 || day==21 || day==31 || day==41 ......){
    result="dan";
}
else{
    result="dana";
}

我怎么能用每个以一个结尾的数字来做到这一点,当然没有写所有数字?

How can i do that with every number that ends with one and of course without writing all numbers?

推荐答案

只需检查除以10的除法:

Just check the remainder of division by 10:

if (day % 10 == 1) { 
  result = "dan";
} else {
  result = "dana";
}

是Modulo或模数运算符,除非您使用JavaScript,在这种情况下它是一个简单的余数运算符(不是真正的模数)。它将这两个数字分开,并返回余数。

% is the "Modulo" or "Modulus" Operator, unless you're using JavaScript, in which case it is a simple remainder operator (not a true modulo). It divides the two numbers, and returns the remainder.

这篇关于如果数字以1结尾做某事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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