java广告墙代码和运行结果
时间: 2024-03-23 17:42:33 浏览: 15
以下是一个简单的Java广告墙程序,使用了Course类:
```
public class AdWall {
public static void main(String[] args) {
Course[] courses = new Course[4];
courses[0] = new Course("Java", "Learn Java programming", 4.5);
courses[1] = new Course("Python", "Introduction to Python", 3.9);
courses[2] = new Course("Web Design", "Create beautiful websites", 4.2);
courses[3] = new Course("Data Science", "Analyze and visualize data", 4.8);
System.out.println("Welcome to our course selection!");
System.out.println("Here are our top-rated courses:");
for (int i = 0; i < courses.length; i++) {
System.out.println(courses[i].getTitle() + " - " + courses[i].getDescription() + " (Rating: " + courses[i].getRating() + ")");
}
}
}
class Course {
private String title;
private String description;
private double rating;
public Course(String title, String description, double rating) {
this.title = title;
this.description = description;
this.rating = rating;
}
public String getTitle() {
return this.title;
}
public String getDescription() {
return this.description;
}
public double getRating() {
return this.rating;
}
}
```
运行结果:
```
Welcome to our course selection!
Here are our top-rated courses:
Java - Learn Java programming (Rating: 4.5)
Python - Introduction to Python (Rating: 3.9)
Web Design - Create beautiful websites (Rating: 4.2)
Data Science - Analyze and visualize data (Rating: 4.8)
```
这个程序首先创建了一个包含4个Course对象的数组,然后遍历这个数组并输出每个Course对象的标题、描述和评分。