简体   繁体   English

更改已在循环外声明的变量

[英]changing a variable that has been declared outside of a loop

I have a program that needs to count the number of leap years that occur between two years.我有一个程序需要计算两年之间发生的闰年的数量。 I have a loop that goes through every loop in between the two selected years, calculates whether they are a leap year, and if they are, it increases a variable called count.我有一个循环遍历两个选定年份之间的每个循环,计算它们是否是闰年,如果是,它会增加一个名为 count 的变量。 By the end I attempted to print the variable to display my answer but it told me the variable had not been initialized.最后,我尝试打印变量以显示我的答案,但它告诉我变量尚未初始化。 So i declared it with a value of zero at the beginning of the program, but now it will only display a zero, and the changes made to the variable stay contained within the loop.所以我在程序开始时用零值声明它,但现在它只会显示零,并且对变量所做的更改包含在循环中。 I don't want to put the print statement inside the loop because that will just cause it to print a bunch of numbers over and over again.我不想将 print 语句放在循环中,因为这只会导致它一遍又一遍地打印一堆数字。 What should I do to fix this.我应该怎么做才能解决这个问题。

public class Main {
  public static void main(String[] args) {
    int year1 = 1808;
    int year2 = 1805;
    boolean leap;
    int count = 0;

    for (int b = 0; b < 1; b++) {
      for (int i = year1; i <= year2; i++) {
        if (i % 4 == 0) {
          leap = true;
        }
        if (i % 4 == 0 && i % 100 == 0) {
          leap = false;
        }
        if (i % 100 == 0 && i % 400 == 0) {
          leap = true;
        }
        if (leap = true) {
          count++;
        }
      }

      System.out.print(count);
    }
  }
}

First of all, I encourage you to adapt unit testing practices for this kind of scenarios.首先,我鼓励您针对这种场景调整单元测试实践。 For this i'm going to give you an example of how to design your code to let you test it properly.为此,我将为您提供一个示例,说明如何设计代码以让您正确测试它。

You current code:您当前的代码:

  • your loop is not being executed, year1 is greater than year2 so i <= year2 is always false.你的循环没有被执行, year1大于year2所以i <= year2总是假的。 That's why.这就是为什么。

i recoment you to rewrite your code as:我建议您将代码重写为:

public class Main {

    public boolean isLeapYear(int year) {
      boolean isLeap = false;
      if (i % 4 == 0) {
        isLeap = true;
      }
      if (i % 4 == 0 && i % 100 == 0) {
        isLeap = false;
      }
      if (i % 100 == 0 && i % 400 == 0) {
        isLeap = true;
      }
      return isLeap;
    }
    
    public int getLeapYears(int firstYear, int secondYear) {
      int count = 0;
      int tmp;

      // firstYear must be less than secondYear
      if (firstYear > secondYear) {
        tmp = firstYear;
        firstYear = secondYear;
        secondYear = tmp;
      }

      while(firstYear <= secondYear) {
        if (isLeapYear(firstYear)) {
          ++count;
        }
        ++firstYear;
      }        
    
      return count;
    }
  
    public static void main(String[] args) {
      int year1 = 1805;
      int year2 = 1808;
    
      System.out.print(getLeapYears(year1, year2));
    }
  }

i've splitted your code in 3 differents functions:我已将您的代码拆分为 3 个不同的功能:

  • main, that will just pass arguments to the main function主要,这只会将 arguments 传递给主要 function
  • getLeapYears, with the responsibility of loop through years and count leap ones getLeapYears,负责循环年数并计算闰年
  • isLeapYear, with the responsibility of knowing if a given year is a leap year or not isLeapYear,负责了解给定年份是否为闰年

Why this??为什么这个?? This design of your code will allow you to write unit tests and obtain information about where are your code errors if they appears.这种代码设计将允许您编写单元测试并获取有关代码错误出现在哪里的信息。

If you have separated functions, even if you don't write unit tests you can try your different functions from your main function and know what's going on:)如果您有分离的功能,即使您不编写单元测试,您也可以从您的主要 function 尝试不同的功能并知道发生了什么:)

You inner for loop is never executed, that's why.您的内部 for 循环永远不会执行,这就是原因。

year1 is greater than year2 , so i <= year2 is never true. year1大于year2 ,所以i <= year2永远不会为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM