计数器如何工作? / 非常基础的 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43900032/
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-11-03 07:48:23  来源:igfitidea点击:

How the counter works? / Very basic Java

javacounter

提问by StraDis

I have a code from a book with a counter in it. I don't understand why it works the way it does. To be specific, how does it happen that counter counts the lines generated by "for" loop? I see that there is an "if" loop with relational operator and conditional expression but it's still isn't clear to me how the code "knows" to count the lines. Here is a code :

我有一本书的代码,里面有一个计数器。我不明白为什么它的工作方式如此。具体来说,计数器如何计算“for”循环生成的行数?我看到有一个带有关系运算符和条件表达式的“if”循环,但我仍然不清楚代码如何“知道”计算行数。这是一个代码:

*/  
class GalToLitTable {  
public static void main(String args[]) {  
double gallons, liters; 
int counter; 

counter = 0; 
for(gallons = 1; gallons <= 100; gallons++) { 
  liters = gallons * 3.7854; // convert to liters 
  System.out.println(gallons + " gallons is " + 
                     liters + " liters."); 

  counter++; 
  // every 10th line, print a blank line        
  if(counter == 10) { 
    System.out.println(); 
    counter = 0; // reset the line counter 
   } 
  } 
 }   

Any help would be much appreciated

任何帮助将非常感激

回答by Rion Williams

There are three things going on here:

这里发生了三件事:

  • You have a forloop that is going to execute 100 times (from 1-100)
  • Within the loop, you'll increment your counter via the increment operator ++, which is essentially the same as calling counter = counter + 1;.
  • Within your loop (after the increment), you'll check the current value to see if you should perform some action (in this case, resetting the counter).
  • 您有一个for要执行 100 次的循环(从 1-100)
  • 在循环中,您将通过 increment operator 增加您的计数器++,这与调用counter = counter + 1;.
  • 在您的循环中(在增量之后),您将检查当前值以查看是否应该执行某些操作(在这种情况下,重置计数器)。

You can see some annotated code below, which explains this a bit better, but I'd highly encourage you to see the links provided above for a bit more detail on the forloop and incrementoperator:

您可以在下面看到一些带注释的代码,这更好地解释了这一点,但我强烈建议您查看上面提供的链接,以了解有关for循环和increment运算符的更多详细信息:

// Your counter
int counter = 0; 

// The contents of this will execute 100 times
for(gallons = 1; gallons <= 100; gallons++) { 

    // Omitted for brevity

    // This increases your counter by 1
    counter++; 

    // Since your counter is declared outside of the loop, it is accessible here
    // so check its value
    if(counter == 10) { 

         // If it is 10, then reset it
         System.out.println(); 
         counter = 0; 
    } 

    // At this point, the loop is over, so go to the next iteration
} 

回答by Musti

counter ++;means counter = counter + 1;This line of code increments the counter by one on every iteration of the foor loop. The If-Clause, resets the counter to zero, when it has reached 10, as you see in counter = 0;. So after 10 iterations the counter reaches 10 and the condition of the if clause is true.

counter ++;意味着counter = counter + 1;这行代码在 for 循环的每次迭代中将计数器加一。If-Clause 将计数器重置为零,当它达到 10 时,如您在 中看到的counter = 0;。因此,在 10 次迭代后,计数器达到 10,并且 if 子句的条件为真。