简体   繁体   中英

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. 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. 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. 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:

  • main, that will just pass arguments to the main function
  • getLeapYears, with the responsibility of loop through years and count leap ones
  • isLeapYear, with the responsibility of knowing if a given year is a leap year or not

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:)

You inner for loop is never executed, that's why.

year1 is greater than year2 , so i <= year2 is never true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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