Android Java数学逻辑问题
如果球员得到了一个特定的分数,我想通知他 我目前正在使用的Android Java数学逻辑问题,java,android,Java,Android,如果球员得到了一个特定的分数,我想通知他 我目前正在使用的 if(score <= 4){ messagePlayer("You scored 1."); } else if(score <= 19){ messagePlayer("You scored 1."); messagePlayer("You scored 5."); } else if(score <= 49){ messagePlayer("You scored 1."); messagePlaye
if(score <= 4){
messagePlayer("You scored 1.");
} else if(score <= 19){
messagePlayer("You scored 1.");
messagePlayer("You scored 5.");
} else if(score <= 49){
messagePlayer("You scored 1.");
messagePlayer("You scored 5.");
messagePlayer("You scored 20.");
} else if(score <= 99){
messagePlayer("You scored 1.");
messagePlayer("You scored 5.");
messagePlayer("You scored 20.");
messagePlayer("You scored 50.");
} else if(score >= 100){
messagePlayer("You scored 1.");
messagePlayer("You scored 5.");
messagePlayer("You scored 20.");
messagePlayer("You scored 50.");
messagePlayer("You scored 100.");
}
if(score现在存在的程序将始终告诉用户他们得了1分,只要他们至少得了1分,因为你的if-else永远不会通过第一个if,它将计算为true
更好的使用方法是,在用户越过该阈值时,循环通过您的目标分数,每次都会得到一条消息:
int[] targets = {0,1,5,20,50,100}
for (int i : targets) {
if (score >= i) {
messagePlayer("You scored " + i + ".")
}
}
现在存在的程序总是会告诉用户他们得了1分,只要他们至少得了1分,因为你的if-else永远不会通过第一个if,而第一个if将被评估为true
更好的使用方法是,在用户越过该阈值时,循环通过您的目标分数,每次都会得到一条消息:
int[] targets = {0,1,5,20,50,100}
for (int i : targets) {
if (score >= i) {
messagePlayer("You scored " + i + ".")
}
}
像这样显式排除零:
if(score > 0 && score <= 4){
messagePlayer("You scored 1.");
}
像这样显式排除零:
if(score > 0 && score <= 4){
messagePlayer("You scored 1.");
}
您可以使此代码更具可读性:
if(score <= 4)
messagePlayer("You scored 1.");
if(score <= 19)
messagePlayer("You scored 5.");
if(score <= 49)
messagePlayer("You scored 20.");
if(score <= 99)
messagePlayer("You scored 50.");
if(score >= 100)
messagePlayer("You scored 100.");
if(score您可以使此代码更具可读性:
if(score <= 4)
messagePlayer("You scored 1.");
if(score <= 19)
messagePlayer("You scored 5.");
if(score <= 49)
messagePlayer("You scored 20.");
if(score <= 99)
messagePlayer("You scored 50.");
if(score >= 100)
messagePlayer("You scored 100.");
if(score所以您还需要检查第一个if语句是否有高于零的结果
if(score>0 && score <= 4)
if(score>0&&score所以您还需要检查第一个if语句是否有高于零的结果。您可以像这样更改第一个if语句
if(score>0 && score <= 4)
if(score>0&&score我将稍微修改一下您的结构
if( score >0){
messagePlayer("You scored 1.");
}
if(score >4){
messagePlayer("You scored 5.");
}
if(score > 19){
messagePlayer("You scored 20.");
}
if(score > 49){
messagePlayer("You scored 50.");
}
if(score > 99){
messagePlayer("You scored 100.");
}
请注意,我删除了else
语句我将稍微修改您的结构
if( score >0){
messagePlayer("You scored 1.");
}
if(score >4){
messagePlayer("You scored 5.");
}
if(score > 19){
messagePlayer("You scored 20.");
}
if(score > 49){
messagePlayer("You scored 50.");
}
if(score > 99){
messagePlayer("You scored 100.");
}
注意,我删除了else
statements做一些JUnit测试怎么样?我不能做很多测试,因为messagePlayer方法只向玩家发送一次消息,然后保存他在首选项中得到的消息,然后如果他下次得分相同,那么它将被忽略,这就是为什么他可以为两个得分都收到消息的原因..那么做s呢ome JUnit测试?我不能做很多测试,因为messagePlayer方法只向玩家发送一次消息,然后保存他在首选项中收到的消息,然后如果他下次得分相同,那么它将被忽略,这就是为什么他可以为两个得分收到消息..这是1,所以我可以对其他得分使用相同的消息?就像如果(分数>4&&score 19&&score那是1的分数,所以我可以对其他的分数使用相同的分数?比如if(分数>4&&score 19&&score,但是如果我的分数为75。)转到if(分数>19){或if(分数>49){或if(分数>4){或if(分数>0)会不会感到困惑{?@JohnJared它将满足所有条件并显示所有消息,与您之前的操作相同,但如果我的分数为75..转到if(分数>19){或if(分数>49){或if(分数>4){或if(分数>0)会不会感到困惑{?@JohnJared它将满足所有条件并显示所有消息,与您以前的情况相同,但可读性不强,事实上编码风格被许多人认为是不好的编码实践,并且容易出错。这不是更具可读性,事实上编码风格被许多人认为是不好的编码实践,并且容易出错。