- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是编程新手,曾尝试编写 if-else 逻辑来创建数组等。
我想使用一个点变量来决定变量落在哪个点区间内,然后用该区间的函数创建数组并从该数组返回一个随机函数。
例如,我有里程碑 1000、2500 等。如果 userScorePoints 超过 2500,我希望该方法从包含关于该数字的 funfact 的数组中返回一个随机 funfact,直到 userScorePoints 达到下一个里程碑,即 5000。
我写的代码的问题是它只从第一个 if 返回一个随机的 funfact,所以我只从数字 1000 中得到 funfact,尽管我应该从数字 2500 中得到 funfact,因为我现在的分数是超过 2603..
有人可以帮我解决这个问题吗?
这是我的代码:
function getFunfact(userScorePoints) {
var array = new Array();
if (1000 <= userScorePoints < 2500) {
var funfact1000 = new Array();
funfact1000[0] = "funfacts about the number 1000";
funfact1000[1] = "...";
funfact1000[2] = "...";
funfact1000[3] = "...";
funfact1000[4] = "...";
funfact1000[5] = "...";
array = funfact1000;
} else if (2500 <= userScorePoints < 5000) {
var funfact2500 = new Array();
funfact2500[0] = "funfacts about the number 2500";
funfact2500[1] = "...";
funfact2500[2] = "...";
funfact2500[3] = "...";
funfact2500[4] = "...";
funfact2500[5] = "...";
array = funfact2500;
} else if (5000 <= userScorePoints < 10000) {
var funfact5000 = new Array();
funfact5000[0] = "funfacts about the number 5000";
funfact5000[1] = "...";
funfact5000[2] = "...";
funfact5000[3] = "...";
funfact5000[4] = "..."
funfact5000[5] = "...";
array = funfact5000;
} else if (10000 <= userScorePoints < 20000) {
var funfact10000 = new Array();
funfact10000[0] = "funfacts about the number 10.000";
funfact10000[1] = "...";
funfact10000[2] = "...";
funfact10000[3] = "...";
funfact10000[4] = "...";
funfact10000[5] = "...";
array = funfact10000;
} else if (20000 <= userScorePoints < 30000) {
var funfact20000 = new Array();
funfact20000[0] = "funfacts about the number 20.000";
funfact20000[1] = "...";
funfact20000[2] = "...";
funfact20000[3] = "...";
funfact20000[4] = "...";
funfact20000[5] = "...";
array = funfact20000;
} else if (30000 <= userScorePoints < 50000) {
//etc.
} else {}
return array[getRandom(6)]; //this method returns a random element, this one works.
最佳答案
你不能像那样链接关系比较。你必须写:
if (1000 <= userScorePoints && userScorePoints < 2500) {
...
}
你写的内容被解析为就像你写的一样:
if ((1000 <= userScorePoints) < 2500) {
...
}
括号中的比较结果为 0 或 1,总是小于 2500。
关于javascript if else逻辑不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18528330/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!