gpt4 book ai didi

java - 错误说我的方法未定义

转载 作者:行者123 更新时间:2023-12-01 06:35:21 24 4
gpt4 key购买 nike

好的,我正在关注 BrandonioProdtuctions 上的 Java 教程YouTube channel 和。我正在学习第 7 部分:面向对象编程简介。我遇到的问题是,当我尝试运行该程序时,它在我直接粘贴在下面的类(标题为 objectIntroTest)中给出错误。

public class objectIntroTest {
public static void main(String[] args){
String x = "Hello";
objectIntro waterBottle = new objectIntro(0);
waterBottle.addwater(100);
waterBottle.drinkWater(20);
System.out.println("Your remaining water level is:"* + waterBottle.getWater());
}
}

这是另一个类,标题为“objectIntro”:

public class objectIntro {

public objectIntro(){
//Default constructor
}
public objectIntro(int waterAmount){
twater = waterAmount;
}

int twater = 0; //This is how much water is in the water bottle
public void addWater(int amount){
twater = twater + amount;
}
public void drinWater(int amount){
twater = twater - amount;
}
public int getWater(){
return twater;
}
}

这是当我尝试运行该程序时它给我的错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method addwater(int) is undefined for the type objectIntro
The method drinkWater(int) is undefined for the type objectIntro
The operator * is undefined for the argument type(s) String, int

at objectIntroTest.main(objectIntroTest.java:6)

为什么会发生这种情况?

最佳答案

你有3个错误:
首先:从此处删除 '*' : System.out.println("您的剩余水位为:"* + waterBottle.getWater());

第二:您在objectIntro中的方法是addWater,您已将其用作waterBottle.addwater(100);(“W”必须是大写)

第三个:objectIntro中的另一个方法是drinWater,但您再次使用错误:waterBottle.drinkWater(20); (额外的“k”)

在编译之前要更加小心并检查运行时错误。

这是一个有关命名约定的网站:
http://java.about.com/od/javasyntax/a/nameconventions.htm

关于java - 错误说我的方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17915605/

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