gpt4 book ai didi

java 无法应用于给定类型

转载 作者:行者123 更新时间:2023-12-01 17:14:02 27 4
gpt4 key购买 nike

所以我得到了这个错误:

java cannot be applied to given types; required: java.lang.String,int,int; found:java.lang.String; reason: actual and formal argument lists differ in lenghhs

当我在一个 Actor 中输入此内容时(我使用的是 Greenfoot):

 public String getPunkte()
{
String stringPunkte = Integer.toString(punkte);
return(stringPunkte);
}

另一个是这个:

public void malePunkte()
{
GreenfootImage img = new GreenfootImage(50, 10);
img.drawString(getPunkte());

}

给那些不明白的人:这应该将 int (punkte)(德语中的点)转换为字符串,然后将一个 Actor 的点数返回给另一个 Actor ,然后显示该数字。

如果您仍然不明白或者需要另一段代码,请询问。

谢谢

最佳答案

嗯,错误是不言自明的,不是吗?

您期望返回的是String,int,int,而您只“提供”String

试试这个

public void malePunkte() {
GreenfootImage img = new GreenfootImage();
img.drawString(getPunkte(), 50, 10);
}

ps。在 return 语句中,您不需要用括号括住要返回的变量。就这样做

return stringpunkte

ps。我不知道 GreenfootImage 是什么;)

编辑:根据 - Gyro Gearless 提供的有用链接

www.greenfoot.org/files/javadoc/greenfoot/GreenfootImage.html

drawImage(GreenfootImage image, int x, int y) 
Draws the given Image onto this image

GreenfootImage(int width, int height) 
Create an empty (transparent) image with the specified size.

如您所见,drawImage 在创建的“空”图像之上绘制。因此,作为构造函数参数,您可以指定空图像的大小,并且对于方法,您可以指定将位于空图像之上的新图像的大小

关于java 无法应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com