类型不匹配:不能从int转换为boolean

时间:2023-01-12 13:10:46

Well, I've tried to solve this problem but I can not, if someone can help me ...

我试着解决这个问题,但如果有人能帮我,我就无能为力了。

CODE:

代码:

package com.skybox.runordie;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {

  public static final long serialVersionUID = 1L;

  public static final int WIDTH = 160;
  public static final int HEIGHT = WIDTH / 12 * 9;
  public static final int SCALE = 3;
  public static final String NAME = "Run Or Die";

  private JFrame frame;

  public boolean running = false;
  public int tickCount = 0;

  private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
  private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();


  public Game() {
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    frame = new JFrame(NAME);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.add(this, BorderLayout.CENTER);
    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }


  public synchronized void start() {
    running = true;
    new Thread(this).start();
  }

  public synchronized void stop() {
    running = false;
  }

  public void run() {
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D/60D;

    int ticks = 0;
    int frames = 0;

    long lastTimer = System.currentTimeMillis();
    double delta = 0;

    while(running) {
      long now = System.nanoTime();
      delta += (now - lastTime) / nsPerTick;
      lastTime = now;
      boolean shouldRender = true;

      while (delta >= 1) {
        ticks++;
        tick();
        delta -= 1;
        shouldRender = true;
      }
      try {
        Thread.sleep(2);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (shouldRender) {
         frames++;
         render();
      }
      if(System.currentTimeMillis() - lastTimer > 1000) {
        lastTimer += 1000;
        System.out.println(frames + " frames," + ticks + " ticks.");
        frames = 0;
        ticks = 0;
      }
    }
  }

  public void tick() {
    tickCount++;
    for(int i = 0; pixels.length; i++) {
      pixels[i] = i + tickCount;
    }
}

  public void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null) {
      createBufferStrategy(3);
      return;
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());

    g.dispose();
    bs.show();
  }

  public static void main(String[] args) {
    new Game().start();
  }


  public int[] getPixels() {
    return pixels;
  }


  public void setPixels(int[] pixels) {
    this.pixels = pixels;
  }

}

2 个解决方案

#1


5  

On what line do you get the error? If I were to guess, it's right here:

在哪条线上你会得到误差?如果我猜一下,它就在这里:

for(int i = 0; pixels.length; i++) {
    pixels[i] = i + tickCount;
}

The second clause in a for statement should be a continuing condition for the loop, which is a boolean. However, pixels.length isn't a boolean, it's an integer. I assume your intent is to compare that integer with another integer, resulting in a boolean:

for语句中的第二个子句应该是循环的连续条件,它是一个布尔值。然而,像素。长度不是布尔值,它是整数。我假设您的目的是将该整数与另一个整数进行比较,得到一个布尔值:

for(int i = 0; i < pixels.length; i++) {
    pixels[i] = i + tickCount;
}

Just supplying an integer by itself doesn't tell the loop anything about the continuing condition. It needs to know on what condition to continue the loop (otherwise the loop should end). That condition, in this case, is when i is less than pixels.length.

仅提供一个整数本身并不会告诉循环关于连续条件的任何信息。它需要知道在什么条件下继续循环(否则循环应该结束)。这种情况下,当我小于像素时。

#2


0  

The problem is in following method

问题就在下面

public void tick() {

           tickCount++;

            for(int i = 0; pixels.length; i++) { // should compare pixels.length to some int to get boolean return

                    pixels[i] = i + tickCount;

            }

#1


5  

On what line do you get the error? If I were to guess, it's right here:

在哪条线上你会得到误差?如果我猜一下,它就在这里:

for(int i = 0; pixels.length; i++) {
    pixels[i] = i + tickCount;
}

The second clause in a for statement should be a continuing condition for the loop, which is a boolean. However, pixels.length isn't a boolean, it's an integer. I assume your intent is to compare that integer with another integer, resulting in a boolean:

for语句中的第二个子句应该是循环的连续条件,它是一个布尔值。然而,像素。长度不是布尔值,它是整数。我假设您的目的是将该整数与另一个整数进行比较,得到一个布尔值:

for(int i = 0; i < pixels.length; i++) {
    pixels[i] = i + tickCount;
}

Just supplying an integer by itself doesn't tell the loop anything about the continuing condition. It needs to know on what condition to continue the loop (otherwise the loop should end). That condition, in this case, is when i is less than pixels.length.

仅提供一个整数本身并不会告诉循环关于连续条件的任何信息。它需要知道在什么条件下继续循环(否则循环应该结束)。这种情况下,当我小于像素时。

#2


0  

The problem is in following method

问题就在下面

public void tick() {

           tickCount++;

            for(int i = 0; pixels.length; i++) { // should compare pixels.length to some int to get boolean return

                    pixels[i] = i + tickCount;

            }