1

我在 Java 中,我想知道我该怎么做。这是我到目前为止的一个小例子,但我必须用 50 做同样的事情。所以问题来了。

if(taskCompleted/10 == Math.round(Math.random()){
taskPoints = 20;
} else if(taskCompleted/50 == Math.round(Math.random()){
taskPoints = 60;
}

否则我可以用word来解释:

每次 taskCompleted 等于 10 的倍数(10,20,30,40 等...),taskPoints 等于 20,否则如果每次 taskCompleted 等于 50 的倍数(50,100,150,200),则 taskPoints 等于 60。

我希望你明白我想做什么。

4

4 回答 4

6
if(taskCompleted % 50 == 0){
   taskPoints = 60;
} else if(taskCompleted % 10 == 0){
   taskPoints = 20;
}

使用将返回余数的模数。这将解决你的问题。仔细查看条件,我在检查 10 之前检查 50。因为 50 的任何倍数也是 10 的倍数。所以如果你放 10,你永远不会达到 50。

于 2012-04-29T06:31:36.123 回答
4
if(taskCompleted % 50 == 0){
       //do something
}else if(taskCompleted % 10 == 0){
       //do something
}
于 2012-04-29T06:31:06.727 回答
3

类似于以前的答案,除了嵌套它们更有效。它不能是 50 的倍数,除非它是 10 的倍数。

if (taskCompleted % 10 == 0)
   taskPoints = taskCompleted % 50 == 0 ? 60 : 20;
于 2012-04-29T08:04:16.923 回答
2
if(taskCompleted % 10 == 0) { 
    taskPoints = 20;
}
if(taskCompleted % 50 == 0) { 
    taskPoints = 60;
}
于 2012-04-29T06:33:19.707 回答