gpt4 book ai didi

java - 错误不兼容类型 : possible lossy conversion from double to long?

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:04 25 4
gpt4 key购买 nike

我制作的游戏出现问题。当尝试编译代码时,我收到一个奇怪的错误,即这篇文章的标题。任何关于它可能意味着什么的帮助。它告诉我要查看的错误是我尝试调用绘图线程的位置。抛出错误的代码是 -- Try Thread.sleep(time);

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Stacker
extends JFrame
implements KeyListener
{
int iteration = 1;
static double time = 200.0D;
static int last = 0;
static int m = 10;
static int n = 20;
JLabel[][] b;
static int[] length = { 5, 5 };
static int layer = 19;
static int[] deltax = new int[2];
static boolean press = false;
static boolean forward = true;
static boolean start = true;

public static void main(String[] args)
{
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
new Stacker();
}

public Stacker()
{
setDefaultCloseOperation(3);
this.b = new JLabel[m][n];
setLayout(new GridLayout(n, m));
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++)
{
this.b[x][y] = new JLabel(" ");
this.b[x][y].setBackground(Color.white);
add(this.b[x][y]);
this.b[x][y].setEnabled(true);
this.b[x][y].setOpaque(true);
this.b[x][y].setBorder(BorderFactory.createLineBorder(Color.GRAY));
this.b[x][y].setPreferredSize(new Dimension(40, 30));
}
}
setFocusable(true);
addKeyListener(this);
pack();
setVisible(true);
go();
}

public void go()
{
int tmp = 0;
Component temporaryLostComponent = null;
do
{
if (forward) {
forward();
} else {
back();
}
if (deltax[1] == 10 - length[1]) {
forward = false;
} else if (deltax[1] == 0) {
forward = true;
}
draw();
try
{
Thread.sleep(time);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} while (!

press);
if (layer > 12) {
time = 150 - (this.iteration * this.iteration * 2 - this.iteration);
} else {
time -= 2.2D;
}
this.iteration += 1;
layer -= 1;
press = false;
tmp = check();
length[0] = length[1];
length[1] = tmp;
if ((layer == -1) && (length[1] > 0))
{
JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations! You beat the game!");
System.exit(0);
}
if (length[1] <= 0)
{
JOptionPane.showMessageDialog(temporaryLostComponent, "Game over! You reached line " + (18 - layer) + "!");
System.exit(0);
}
last = deltax[1];
start = false;
go();
}

public int check()
{
if (start) {
return length[1];
}
if (last < deltax[1])
{
if (deltax[1] + length[1] - 1 <= last + length[0] - 1) {
return length[1];
}
return length[1] - Math.abs(deltax[1] + length[1] - (last + length[0]));
}
if (last > deltax[1]) {
return length[1] - Math.abs(deltax[1] - last);
}
return length[1];
}

public void forward()
{
deltax[0] = deltax[1];
deltax[1] += 1;
}

public void back()
{
deltax[0] = deltax[1];
deltax[1] -= 1;
}

public void draw()
{
for (int x = 0; x < length[1]; x++) {
this.b[(x + deltax[0])][layer].setBackground(Color.white);
}
for (int x = 0; x < length[1]; x++) {
this.b[(x + deltax[1])][layer].setBackground(Color.BLUE);
}
}

public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 32) {
press = true;
}
}

public void keyReleased(KeyEvent arg0) {}

public void keyTyped(KeyEvent arg0) {}
}

最佳答案

这是一个标准的 Java 编译器错误,旨在防止程序员意外使用精度较低的类型。在您的情况下,您尝试将十进制值( double )传递到采用整数类型的方法中(Thread.sleep(milliseconds))

double value = 1.234;
Thread.sleep(value); // Error, value passed would end up being 1, not 1.234
Thread.sleep((long)value); // OK, you've explicity made the cast from 1.234 to 1 yourself

强烈建议您阅读 Java 类型,例如official tutorial .

关于java - 错误不兼容类型 : possible lossy conversion from double to long?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36827677/

25 4 0
文章推荐: html - CSS div/元素位置问题
文章推荐: c++ - 使用点云库 (PCL) 时的 Boost 问题
文章推荐: linux - 为什么 Plex Media Server 在 Ubuntu 16 上不适合我?
文章推荐: html - 让z-index > 0的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com