java - if 语句产生不正确的结果

标签 java if-statement

我正在制作一款可以生成 3 角星 (3PS) 和 4 角星 (4PS) 的游戏。你必须让 3PS 落在右边,4PS 落在左边。我使用 if 语句来检查它们是否已经沿着正确的路线下降,但它无法正常工作。我为 3PS 创建了一个明星类,并创建了一个扩展 3PS 类的 4PS 类。我有一个变量,每当一颗星星落在屏幕下方时,该变量就会递增,3PS 似乎没问题,但 4PS 会递增 4PS 和 3PS 的变量。我该如何解决这个问题?

这是我的代码

           if (0 <= starW && starW <= (.5 * dimension[0]) && starH <= 0) {
                    if (star[a] instanceof fourStar) {
                        if (count4Check == false) {
                        count4C++;
                        count4Check = true;
                        starAdded = 0;
                        Log.i("count4C", String.valueOf(count4C));
                   }count4Check = false;
                }
            }
            if (0 <= starW && starW <= (.5 * dimension[0]) && starH <= 0) {
                    if (star[a] instanceof Star) {
                        if (count3Check == false) {
                        count3W++;
                        count3Check = true;
                        starAdded = 0;
                        Log.i("count3W", String.valueOf(count3W));
                   }count3Check = false;
                }
            }
            if ((.5 * dimension[0]) <= starW && starW <= dimension[0] && starH <= 0) {
                    if (star[a] instanceof Star) {
                        if (count3Check == false) {
                        count3C++;
                        count3Check = true;
                        starAdded = 0;
                        Log.i("count3C", String.valueOf(count3C));
                    }count3Check = false;
                }
            }
            if ((.5 * dimension[0]) <= starW && starW <= dimension[0] && starH <= 0) {
                    if (star[a] instanceof fourStar) {
                        if (count4Check == false) {
                        count4W++;
                        count4Check = true;
                        starAdded = 0;
                        Log.i("count4W", String.valueOf(count4W));
                    }count4Check = false;
                }
            }

这是日志猫

03-21 20:06:36.620  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:39.623  24716-25725/com.example.james.sata I/count3C﹕ 9
03-21 20:06:39.713  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:42.276  24716-25725/com.example.james.sata I/count4C﹕ 2
03-21 20:06:42.276  24716-25725/com.example.james.sata I/count3W﹕ 5
03-21 20:06:42.356  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:45.149  24716-25725/com.example.james.sata I/count3W﹕ 6
03-21 20:06:45.239  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:48.592  24716-25725/com.example.james.sata I/count4C﹕ 3
03-21 20:06:48.592  24716-25725/com.example.james.sata I/count3W﹕ 7
03-21 20:06:48.692  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:51.726  24716-25725/com.example.james.sata I/count4C﹕ 4
03-21 20:06:51.726  24716-25725/com.example.james.sata I/count3W﹕ 8
03-21 20:06:51.776  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:54.779  24716-25725/com.example.james.sata I/count3C﹕ 10
03-21 20:06:54.859  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:07:10.446  24716-25725/com.example.james.sata I/count3C﹕ 11
03-21 20:07:10.526  24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:07:25.772  24716-25725/com.example.james.sata I/count3C﹕ 12
03-21 20:07:25.772  24716-25725/com.example.james.sata I/count4W﹕ 3

最佳答案

四星是星的子类,所以它也会通过 if

if (star[a] instanceof Star) {

例如,您可以有代码:

boolnea isOnLeft = starW <= (.5 * dimension[0]);
boolean isOut = starH <= 0;

if (star[a] instanceof fourStar) {
  if (isOnLeft && isOut) {
  ...
  }
} else { //3ps
  if (!isOnLeft && isOut) {
  ...
  }
}

尽管如此,如果你检查实际的类,它并不是很面向对象。 您应该拥有具有两个子类别 3PS 和 4PS 的 Star 类别。然后在star类中定义抽象方法boolean ShouldCount(position),在3PS中你实现shouldCount(),如果位置在右侧并且在屏幕之外则返回true,在4PS中则相反。然后您只需调用 shouldCount 并增加分数即可。

关于java - if 语句产生不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187627/

相关文章:

java - Jersey @GET 方法不适用于 2 个路径参数

javascript - 我的JavaScript自定义错误消息拒绝清除,即使条件变为true

JavaScript:如果 var?其中 var 不是 bool

java - 将全局变量传递给对象

java - 异常: Caused by: org. springframework.beans.factory.BeanCreationException:创建名称为 'httpPutFormContentFilter'的bean时出错

java - @JoinColumn 注解中名称和引用列名的区别?

ruby-on-rails - Rails 回调 : Use if more than once as conditional in a callback

python - 编写 Long "if"语句的更好方法?

Javascript: "else if"语句有限制吗?

java - -cp ./为什么我在运行 Java 时总是必须手动将 CWD 包含到 CP 中?