gpt4 book ai didi

java - 为什么 Java 中的 void 函数无法返回值?

转载 作者:行者123 更新时间:2023-12-01 07:42:52 25 4
gpt4 key购买 nike

更正以下方法 getAge() 的错误定义

public void getAge() 
{
return age;
}

将您的答案粘贴到此处:

public int getAge(int age) 
{
return age;
}

标记:0 分(满分 1 分)

评论:

* Test 1 (0.0 out of 1)

Person.java:15: getAge(int) in Person cannot be applied to ()
String s = (p.getAge() == 16 ? "getAge() Correct" : "getAge() not Correct");
^
1 error
The output should have been:
getAge() Correct

This is what was actually produced:
Exception in thread "main" java.lang.NoClassDefFoundError: Person

它希望我用字符串替换它而不是 (int Age) 吗?

最佳答案

我相信您正在寻找的正确代码是:

public int getAge() 
{
return age;
}

基本上说 getAge() 不需要任何参数(你的错误是,你传入了一个整数参数)并且它将返回一个 int 类型的值。

关于java - 为什么 Java 中的 void 函数无法返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640673/

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