Java第7次作业
1.
public class Hestia {
public static void main(String[] args) {
int[] arr = {3, 7, 5, 2, 1, 40};//定义数组
int temp = arr[0];//定义一个整形,用于比较和接受最大值
for (int i = 0; i < arr.length; i++) {//遍历数组
if (temp < arr[i]) {//如果temp小,则将遍历到的数复制给temp
temp = arr[i];
}
}
System.out.print(temp);//输出最大值
}
}
2.
public class Book {
private String name;
private int pages;
public void setName(String name) {
this.name = name;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public int getPages() {
return pages;
}
public Book(String name, int pages) {
this.name = name;
this.pages = pages;
}
public Book() {
}
public void display() {
System.out.println("书名:" + getName());
System.out.println("页数:" + getPages());
}
}
public class BookDeom {
public static void main(String[] args) {
Book hestia_book = new Book("Java程序设计", 338);
hestia_book.display();
}
}