定义WeatherDemo类,实现main方法,包含以下功能: 建立一个Weather类的ArrayList列表对象。 从屏幕读入一个整数n,代表天数。 在列表对象中插入n个的Weather对象,即n天的天气情况。 遍历列表,把所有对象的温度、湿度输出(使用for)。 遍历列表,把所有对象的温度、湿度输出(使用增强for)。 遍历列表,把所有对象的温度、湿度输出(使用迭代器)。 遍历列表,统计列表对象中comfortable的天数,并输出。
时间: 2024-03-24 11:41:04 浏览: 17
好的,以下是代码实现:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class WeatherDemo {
public static void main(String[] args) {
ArrayList<Weather> weatherList = new ArrayList<Weather>(); // 建立一个Weather类的ArrayList列表对象
Scanner scanner = new Scanner(System.in);
System.out.print("请输入天数:");
int n = scanner.nextInt(); // 从屏幕读入一个整数n,代表天数
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i + 1) + "天的温度和湿度(用空格隔开):");
double temperature = scanner.nextDouble();
double humidity = scanner.nextDouble();
Weather weather = new Weather(temperature, humidity);
weatherList.add(weather); // 在列表对象中插入n个的Weather对象,即n天的天气情况
}
System.out.println("使用for循环遍历:");
for (int i = 0; i < weatherList.size(); i++) {
System.out.println("第" + (i + 1) + "天的温度为:" + weatherList.get(i).getTemperature() + ",湿度为:" + weatherList.get(i).getHumidity());
}
System.out.println("使用增强for循环遍历:");
for (Weather weather : weatherList) {
System.out.println("温度为:" + weather.getTemperature() + ",湿度为:" + weather.getHumidity());
}
System.out.println("使用迭代器遍历:");
Iterator<Weather> iterator = weatherList.iterator();
while (iterator.hasNext()) {
Weather weather = iterator.next();
System.out.println("温度为:" + weather.getTemperature() + ",湿度为:" + weather.getHumidity());
}
// 统计comfortable的天数
int comfortableDays = 0;
for (Weather weather : weatherList) {
if (weather.isComfortable()) {
comfortableDays++;
}
}
System.out.println("comfortable的天数为:" + comfortableDays);
}
}
```
在上面的代码中,我们首先建立了一个`Weather`类的`ArrayList`列表对象,然后从屏幕读入一个整数`n`,代表天数。接着使用`for`循环向列表对象中插入`n`个`Weather`对象,即`n`天的天气情况。
接下来,我们使用三种不同的方式遍历列表对象,输出所有对象的温度、湿度信息。最后,我们使用一个`for`循环遍历列表对象,统计`comfortable`的天数,并输出。
需要注意的是,上述代码中使用了`Weather`类,我们需要在同一个包中建立一个`Weather`类文件,并在其中定义`Weather`类。以下是`Weather`类的代码实现:
```java
public class Weather {
private double temperature; // 温度
private double humidity; // 湿度
public Weather(double temperature, double humidity) {
this.temperature = temperature;
this.humidity = humidity;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public double getHumidity() {
return humidity;
}
public void setHumidity(double humidity) {
this.humidity = humidity;
}
// 判断天气是否舒适
public boolean isComfortable() {
return temperature >= 20 && temperature <= 28 && humidity >= 40 && humidity <= 60;
}
}
```
在上述代码中,我们定义了`Weather`类,它包含两个私有属性`temperature`和`humidity`,分别代表温度和湿度。我们还提供了一个构造方法和一些访问器方法。
此外,我们还定义了一个`isComfortable()`方法,用于判断天气是否舒适。在本例中,我们将温度在20~28度之间,湿度在40~60%之间的天气定义为舒适的天气。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)