【问题标题】:if Statement not evaluating boolean correctly in Java?如果语句没有在 Java 中正确评估布尔值?
【发布时间】:2013-09-04 07:17:32
【问题描述】:

我正在制作一个简单的游戏,您在屏幕上移动一个球并尝试避开屏幕上的其他方块。

我遇到的问题是,当我尝试评估您是否正在触摸另一个块时,无论我的布尔值等于什么,我的 if 语句总是返回第一个结果。 这是我评估语句的方法

public void actionPerformed(ActionEvent evt) {
                    //Move the ball
            myY = myY - 5;
                if(myY < 0){
                    myY = 0;
                }
                    //test if it is intersecting
            boolean sect = testIntersection(r, playa);
            System.out.println(sect);
                if(sect = true){
                                    cir = Color.BLACK;
                    System.out.println("Intersected");
                }else{
                    cir = Color.RED;
                }
            playa.setFrame(myX, myY, 30,30);
            repaint();
        }

这是发送 sect 布尔值的代码部分:

public static boolean testIntersection(Shape shapeA, Shape shapeB) {
       Area areaA = new Area(shapeA);

       areaA.intersect(new Area(shapeB));
       return !areaA.isEmpty();
    }

这是完整的代码:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.lang.Object;

public class moving extends Canvas implements ActionListener{
Timer up = new Timer(10, this);
Timer down = new Timer(10, this);
Timer left = new Timer(10, this);
Timer right = new Timer(10, this);
int sected;
int myX = 400;
int myY = 400;
Ellipse2D playa = new Ellipse2D.Double(myX, myY, 30, 30);
Rectangle r = new Rectangle(20,33,20, 100);
boolean sect = testIntersection(r, playa);
Color cir = new Color(0,0,0);

public moving() {
    setSize(new Dimension(500, 500));
    final int width = getWidth();
    final int height = getHeight();
    up.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            myY = myY - 5;
                if(myY < 0){
                    myY = 0;
                }
            boolean sect = testIntersection(r, playa);
            System.out.println(sect);
                if(sect = true){
                    System.out.println("intersected!!!!!1");
                }else{
                    cir = Color.RED;
                }
            playa.setFrame(myX, myY, 30,30);
            repaint();
        }
    });
    down.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            myY = myY + 5;
            if(myY > height - 30){
                myY = height - 30;
            }
            boolean sect = testIntersection(r, playa);
            System.out.println(sect);
            if(sect = true){
                System.out.println("intersected");
            }else{
                cir = Color.RED;
            }
            playa.setFrame(myX, myY, 30,30);
            repaint();
        }
    });

    left.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            myX = myX - 5;
            if(myX < 0){
                myX = 0;
            }
            boolean sect = testIntersection(r, playa);
            System.out.println(sect);
            if(sect = true){
                System.out.println("intersected");
            }else{
                cir = Color.RED;
            }
            playa.setFrame(myX, myY, 30,30);
            repaint();
        }
    });
    right.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            myX = myX + 5;
            if(myX > width - 30){
                myX = width - 30;

            }
            boolean sect = testIntersection(r, playa);
            System.out.println(sect);
            if(sect = true){
                System.out.println("intersected");
            }else{
                cir = Color.RED;
            }
            playa.setFrame(myX, myY, 30,30);
            repaint();
        }
    });
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent evt) {
            moveIt(evt);
        }
        public void keyReleased(KeyEvent evt) {
            NomoveIt(evt);
        }
    });
}



public void moveIt(KeyEvent evt) {
 switch (evt.getKeyCode()) {
        case KeyEvent.VK_DOWN:
            down.start();
            break;
        case KeyEvent.VK_UP:
            up.start();
            break;
        case KeyEvent.VK_LEFT:
            left.start();
            break;
        case KeyEvent.VK_RIGHT:
            right.start();
            break;
    }

    repaint();
}

public void NomoveIt(KeyEvent evt) {
    switch (evt.getKeyCode()) {
           case KeyEvent.VK_DOWN:
               down.stop();
               break;
           case KeyEvent.VK_UP:
               up.stop();
               break;
           case KeyEvent.VK_LEFT:
               left.stop();
               break;
           case KeyEvent.VK_RIGHT:
               right.stop();
               break;
       }

       repaint();
   }

public static void main(String[] args) {
    JFrame frame = new JFrame("Basic Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    moving ex = new moving();
    frame.getContentPane().add(ex);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    ex.requestFocus();

}
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    if(sect = true){
        g.setColor(cir);
    }else{
        g.setColor(cir);
    }

    g2.fill(playa);
    g.setColor(Color.black);
    g2.fill(r);
}

public static boolean testIntersection(Shape shapeA, Shape shapeB) {
       Area areaA = new Area(shapeA);

       areaA.intersect(new Area(shapeB));
       return !areaA.isEmpty();
    }

请告诉我我在这里做错了什么!!!

谢谢!

【问题讨论】:

    标签: java if-statement boolean


    【解决方案1】:

    你需要

    if (sect == true) {          // compares sect with true and checks the result
    

    而不是

    if (sect = true) {           // sets sect to true and then checks it
    

    甚至更好会使用

    if (sect) {                 // checks sect
    

    【讨论】:

    • 好吧,我认为 1 等号用于布尔值,因为当我将一个等号用于其他任何东西时,它表示它不能从布尔值转换为 ____(Int、double 等)。
    • @Hiblah200: = 始终是赋值,== 始终是身份比较。
    【解决方案2】:

    您错误地将 true 布尔文字分配给 if 块表达式中的变量:

    if(sect = true)
    

    因此,表达式总是被评估为true。这就是永远不要将布尔变量与truefalse 进行比较的原因。只需在这里使用sect 就足够了。

    改成:

    if (sect)
    

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多