Dogdogs[]=newDog[4];for(inti=0;i<4;i++){dogs[i]=newDog();Scannerin=newScanner(System.in);System.out.println("inputthenameofthe"+(i+1)+"dog");Stringname=in.nextLine();Syst...
Dog dogs[]=new Dog[4];
for(int i=0;i<4;i++){
dogs[i]=new Dog();
Scanner in=new Scanner(System.in);
System.out.println("input the name of the "+(i+1)+" dog" );
String name=in.nextLine();
System.out.println("input weight of the dog ");
float weight=in.nextFloat();
}
float all=0;
for(int i=0;i<4;i++){
all=all+dogs[i].getWeight();
}
float avg=all/dogs.length;
System.out.println("the all weight is "+all);
System.out.println("the average weight is "+avg);
}
}
class Dog{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private float weight;
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
这段代码运行后,为什么all 和 avg都是0呢,哪错了,应该怎么改
展开