Java编程初学者求助:LandVille类构造函数代码补全及理解
Hey there! As a fellow Java learner, I totally get how confusing bits of code can feel at first. Let's walk through fixing and understanding this LandVille class together.
First, here's the complete, working version of your code with missing parts filled in:
public class LandVille { private int[][] land; private boolean hasHouse; public static void main(String args[]) { LandVille landVille = new LandVille(3, 4); landVille.displayLand(); } // Task A - Constructor LandVille(int numRows, int numColumns) { land = new int[numRows][numColumns]; // Initialize all land cells to 0 (representing empty land) for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numColumns; ++j) { land[i][j] = 0; } } // Initially, no house has been built, so set to false hasHouse = false; } // Added: Method to display the current state of the land public void displayLand() { System.out.println("Current Land State (0 = Empty):"); for (int[] row : land) { // Enhanced for-loop (cleaner than nested indexes) for (int cell : row) { System.out.print(cell + " "); } System.out.println(); // New line after each row } // Show house status System.out.println("Has a house been built? " + (hasHouse ? "Yes" : "No")); } }
Breakdown of Key Parts
1. Constructor Fix: hasHouse Initialization
In your original code, the hasHouse variable was left unassigned. Here's why we set it to false:
hasHouseis a private boolean field that tracks whether a house exists on the land.- When you first create a
LandVilleobject, no house has been built yet, so the logical starting value isfalse. - While Java does give boolean fields a default value of
false, explicitly initializing it makes your code easier to read and understand for anyone (including future you!).
2. Land Array Initialization
The nested loops in the constructor:
for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numColumns; ++j) { land[i][j] = 0; } }
- This creates a 2D array with
numRowsrows andnumColumnscolumns, where every cell is set to0. 0here represents "empty land" — you could later expand this to use other numbers (like1for a house,2for a farm) to add more features.
3. Added displayLand() Method
Your main method calls landVille.displayLand();, but this method wasn't defined. I added it to:
- Print out the grid of land cells so you can see the state visually.
- Use an enhanced for-loop (
for (int[] row : land)) which is cleaner than using index variables for simple iteration. - Show the status of
hasHouseso you can confirm if a house exists.
4. Class Fields Explained
private int[][] land: A private 2D array that stores the state of each land cell. Making it private follows the encapsulation principle (keeping internal data hidden from outside code).private boolean hasHouse: A private boolean flag that tracks house presence. Again, this is encapsulation in action — you can later add methods likebuildHouse()to change this value safely.
What Happens When You Run It?
When you execute the code, you'll see this output:
Current Land State (0 = Empty):
0 0 0 0
0 0 0 0
0 0 0 0
Has a house been built? No
This confirms your land grid is initialized correctly, and no house exists yet.
内容的提问来源于stack exchange,提问作者Josi

