import java.util.*;
public class SdutentAarryTest {
public static void main(String[] args) {
Dog dogs[]=new Dog[4];
for(int i= 0;i<4;i++){
dogs[i]=new Dog();
System.out.println("输入第"+(i+1)+"狗名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
dogs[i].setName(name);
System.out.println("输入第"+(i+1)+"狗的重量");
String s_weight=scan.nextLine();
float weight=Float.parseFloat(s_weight);//s
dogs[i].setWeight(weight);
}
float allweight=0;
for(int i=0;i<4;i++){
allweight+=dogs[i].getWeight();
}
System.out.print("总体重="+allweight+"平均体重等于="+allweight/4);
}
class Dog{
private float weight;
private String name;
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
|
|