java 将计数器重置为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4007308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:20:34  来源:igfitidea点击:

Resetting counter to zero

java

提问by user339946

I'm currently learning Java.

我目前正在学习Java。

I have a for loop which uses a counter to keep track of how many words are written on a line.

我有一个 for 循环,它使用一个计数器来跟踪一行上写了多少个单词。

When the wordCount == 10, I insert a line break System.out.println()

当 wordCount == 10 时,我插入一个换行符 System.out.println()

I want to reset wordCount to zero after this line break, but redeclaring wordCount == 0 is "not a statement" in NetBeans. How do I reset a variable value? Thanks

我想在换行后将 wordCount 重置为零,但在 NetBeans 中重新声明 wordCount == 0 是“不是声明”。如何重置变量值?谢谢

回答by Greg Hewgill

Try:

尝试:

wordCount = 0;

Using the double equals sign ==is comparison, while the single equals sign =is assignment.

使用双等号==比较,而单等号=赋值

回答by sethu

Yeah u can add a condition :

是的,你可以添加一个条件:

if ( wordcount ==10) wordcount =0;

如果(字数==10)字数=0;

This will reinitialize wordcount to 0, when it becomes 10. Hope it helps.

这会将 wordcount 重新初始化为 0,当它变为 10 时。希望它有所帮助。

We use '==' during comparison and '=' for assigning values.

我们在比较期间使用 '==' 并使用 '=' 分配值。