Java:无法获取要打印的递增值。
我正在学习一些 Java 101 语法的东西。 在我尝试做的练习中,我无法打印增量值。 有任何想法吗?
这是我的代码:
class StaticTest {
static int i = 47;
}
class incrementable {
static void increment() { StaticTest.i++; }
}
class DataOnly {
int i;
double d;
boolean b;
public static void main (String[] args) {
incrementable t = new incrementable();
DataOnly df = new DataOnly();
df.i = t.increment();
System.out.println(df.i);
}
}
我得到的错误是:
aTyp0eName.java:18: incompatible types
found: void
required: int
df.i = t.increment();
df.i
是一个int
,t.increment
也是如此。 我猜这是因为 increment()
是 void
?
I'm learning to do some Java 101 syntax stuff. In an exercise I'm trying to do, I can't get the incremented value to print. Any ideas?
Here is my code:
class StaticTest {
static int i = 47;
}
class incrementable {
static void increment() { StaticTest.i++; }
}
class DataOnly {
int i;
double d;
boolean b;
public static void main (String[] args) {
incrementable t = new incrementable();
DataOnly df = new DataOnly();
df.i = t.increment();
System.out.println(df.i);
}
}
The error I get is:
aTyp0eName.java:18: incompatible types
found: void
required: int
df.i = t.increment();
df.i
is an int
and so is t.increment
. I'm guessing it's because increment()
is void
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的方法签名是:
这意味着该方法没有返回任何内容。
因此,尝试将
increment()
分配给df.i
将不起作用。your method signature is:
Which means nothing is returned from this method.
Hence, attempting to assign
increment()
todf.i
won't work.increment()
没有返回值。 您需要让它返回一个int
才能使您的代码正常工作:“
void
”是为不返回任何值的方法保留的关键字。 否则,您指定返回什么类或基元(int
、double
、String
等)。 您可以在此处了解有关返回值的更多信息。另外,为了将来的参考,类名应该大写,所以你应该使用“Incrementable”而不是“incrementable”。 (在我的示例代码中,我将其与您的代码保持一致,因此您现在可以将代码放入其中。)
increment()
doesn't have a return value. You'd need to have it return anint
for your code to work:"
void
" is a keyword reserved for methods which don't return any value. Otherwise, you specify what class or primitive you return (int
,double
,String
, whatever). You can read more about return values here.Also, for future reference, class names should be capitalized, so you should be using "Incrementable" rather than "incrementable". (In my sample code I kept it the same as you had it so you could just drop the code in for now.)
Eeeexactly,你的猜测是正确的。
这是逐行解释的错误消息:
You are attempts to allocate "void" to an int in line 18.
这是错误所在。
您在方法“签名”中声明返回类型是什么。
方法签名是:
所以返回类型是void。
下一行:
df.i 如您之前所述是 int 。
所以,你自己的问题基本上已经有了答案。
拥有编译器的好处是,当出现问题时它会告诉您。
坏事(对人类来说)你必须学会阅读这些消息。 它们因编程语言而异,甚至因编译器而异。
这将是一个更正的版本:
还可以进行一些其他增强,例如向方法和属性添加访问修饰符,但现在我认为这会有所帮助。
Eeeexactly, your guess is correct.
This is the error message explained line by line:
You are trying to assign "void" to an int in line 18.
Here is where the error is.
You declare what's the return type in the method "signature".
The method signature is :
So the return type is void.
Next line:
df.i is int as you have previously stated.
So, pretty much you have already your own question answered.
The good point of having a compiler is that it tells you when something is wrong.
The bad thing ( for humans ) you have to learn to read those messages. They vary from programming language to another and even from compiler to compiler.
This would be a corrected version:
There are some other enhancements could be done, such as adding access modifiers to the methods and attributes, but for now I think this would help.
答案是increment()方法返回一个void。 话虽这么说,有很多违反程序员通常使用的Java标准语法的情况,即:
编辑:
同一类中的辅助方法的示例:
The answer is that the increment() method returns a void. That being said, there are a lot of violations of the Java standard syntax that programmers generally use, Namely:
EDIT:
An example of a helper method within the same class:
另外,如果实例化可增量,则不应将方法increment() 定义为静态。 或者你可以,但不需要。
如果它是静态的,你可以简单地使用incrementable.increment()。
只是一个提示。 至于你的问题的答案,toolkit已经说得对了。
also, if you instantiate incrementable, you shouldn't define the method increment() as static. or you can, but don't need to.
you could simply incrementable.increment() if it's static.
just a tip. as for the answer to your question, toolkit already said that right.
另外,您的
incrementable
类确实应该是Incrementable
。Also, your
incrementable
class should really beIncrementable
.