简体   繁体   English

Android Java数学逻辑问题

[英]Android Java math logic issue

I want to message the player if he score a specific score. 我想告诉球员得分是否特定。

What I am currently using .. 我目前正在使用什么..

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.");
}

The scores I want are: 我想要的分数是:

  • 1 Point 1点
  • 5 5
  • 20 20
  • 50 50
  • 100 100

Is my logic 100% right and accurate? 我的逻辑100%正确正确吗? I think my first one score <= 4 is wrong , so if he gets 0 he can also get the message You scored 1 . 我认为我的第一个score <= 4错误的 ,因此,如果他的score <= 40他也可以得到消息, You scored 1 score <= 4You scored 1 And I am not using score == x, because I need to message the player at the end, when he finish, so his highscore like 15. 而且我使用score == x,因为我需要在结束时向玩家发送消息,所以他的高分应该是15。

Explicitly exclude zero like this: 像这样显式排除零:

if(score > 0 && score <= 4){
 messagePlayer("You scored 1.");

}

Or better yet, store your boundary values in an array and solve the problem in one loop: 或者更好的是,将边界值存储在一个数组中,并在一个循环中解决问题:

int[] boundaries = { 1, 5, 20, 50, 100 };

for (int i : boundaries) {
    if (score >= i) {
        messagePlayer("You scored " + i);
    }
}

The program as it exists now will always tell the user they scored 1, as long as they scored at least one, because your if-else will never make it past the first if, which will evaluate to true. 现在存在的程序将始终告诉用户他们获得1分(只要他们至少获得1分),因为if-else永远不会超过第一个if,否则它将为true。

Better usage really would be to loop through your target scores an message each as the user crossed that threshold: 更好的用法实际上是在用户超过该阈值时循环遍历您的目标分数消息:

int[] targets = {0,1,5,20,50,100}

for (int i : targets) {
 if (score >= i) {
  messagePlayer("You scored " + i + ".")
 }
}

You can make this code much more readable: 您可以使此代码更具可读性:

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语句以获取大于零的结果。您可以像这样更改第一个if语句

if(score>0 && score <= 4)

i will modify little bit your structure 我会稍微修改一下您的结构

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.");
}

note that, i removed else statements 请注意,我删除了else语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM