gpt4 book ai didi

JAVA方法返回某条公交路线接载的总人数

转载 作者:行者123 更新时间:2023-11-30 07:26:53 25 4
gpt4 key购买 nike

所以这是我的“Stop”类,其中有一个“station”类型的变量(该类与此事无关)和一个 int 变量“nPeople”,代表正在进入的人数该站公交车:

public class Stop {

private Station station;
private int nPeople;


public Stop(Station station, int nPeople) {
this.station = station;
this.nPeople = nPeople;
}


public Station getStation() {
return station;
}
public void setStation(Station station) {
this.station = station;
}

public int getnPeople() {
return nPeople;
}
public void setnPeople(int nPeople) {
this.nPeople = nPeople;
}
}

现在这是我的“Route”类,其中我有一个“Stop”类型的数组变量,它存储公共(public)汽车所经过的所有站点,使其成为公共(public)汽车路线,我想要一个方法“getTotalPeople”来返回总数巴士在一条路线上搭载的人数:

public class Route {


private Station start;
private Stop[] stops;

public Route(Station start, Stop[] stops) {
this.start = start;
this.stops = stops;
}

public Station getStart() {
return start;
}

public void setStart(Station start) {
this.start = start;
}

public Stop[] getStops() {
return stops;
}

public void setStops(Stop[] stops) {
this.stops = stops;
}



public int getTotalPeople(){

}

}

有什么想法如何返回“Stop[] Stops”数组的“nPeople”变量的总和吗?

最佳答案

public int getTotalPeople(){
int total = 0;
for(int i =0; i < stops.length ; i++){
total += stops[i].getnPeople();
}
return total;
}

关于JAVA方法返回某条公交路线接载的总人数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705902/

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