gpt4 book ai didi

Java变量赋值

转载 作者:行者123 更新时间:2023-12-01 06:32:38 24 4
gpt4 key购买 nike

我的代码打印 0 而不是尺寸和年份。我究竟做错了什么?基本上我希望它返回那些但我不确定我在做什么。

public class House {
private int year;
private int size;
private static int nbrOfHouses;
public static final int MIN_SIZE =10;

public House(int _year,int _size){
_year = year;
_size = size;
}
public static int getNbrHouses(){
return nbrOfHouses;
}
public int getYear(){
return year;
}
public int getSize(){
return size;
}
}


House[] myHouse = new House[10];{
myHouse[0] = new House(1902, 120);
myHouse[1] = new House(1954, 180);
myHouse[2] = new House(1995,90);

for(int i=0; i< myHouse.length; i++){
if(myHouse[i]!=null){
System.out.println(myHouse[i].getYear());

最佳答案

这是倒退的:

public House(int _year,int _size){
_year = year;
_size = size;
}

应该是:

public House(int _year,int _size){
year = _year;
size = _size;
}

或者更好:

public House(int year,int size){
this.year = year;
this.size = size;
}

关于Java变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18565865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com