JAVA第8次实验
1.for遍历:
public class Hestia {
public static void main(String[] args){
int[] arr={2,5,8,1,7};
int min=arr[0];
for (int i = 0; i < arr.length; i++) {
if(min>arr[i]){
min=arr[i];
}
}
System.out.println("最小值:"+min);
}
}
1.foreach遍历:
public class Hestia {
public static void main(String[] args){
int[] arr={2,5,8,1,7};
int min=arr[0];
for (int i : arr) {
if(min > i){
min = i;
}
}
System.out.println("最小值:"+min);
}
}
2.Person类
public class Person {
private String name;
private int age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person() {}
public Person(String name,int age,String address) {
this.name=name;
this.age=age;
this.address=address;
}
public void display(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("地址:"+address);
}
}
3.PersonDemo类
public class PersonDemo {
public static void main(String[] args){
Person p1=new Person("千世",16,"山东省聊城市");
Person p2=new Person("Hestia",19,"江苏省南京市");
System.out.println("年龄较大的是:"+pd(p1,p2));
}
public static String pd(Person a,Person b){
if(a.getAge()>b.getAge()){
return a.getName();
}else{
return b.getName();
}
}
}