为啥 最后总体重 平均都是0啊 急急急
import java.io.*;
public class Demo5_2
{
public static void main(String[] args) throws Exception
{
Dog dogs[]=new Dog[4];
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
for (int i=0;i<4 ;i++ )
{
dogs[i]=new Dog();
System.out.println("请输入"+(i+1)+"只狗名");
String name = br.readLine();
dogs[i].setName(name);
System.out.println("请输入"+(i+1)+"只狗的体重 ");
String s_weight= br.readLine();
float weight= Float.parseFloat(s_weight);
dogs[i].setWeight(weight);
}
float allWeight = 0;
for(int i=0;i<4 ;i++ )
{allWeight+=dogs[i].getWeight();
}
float avgWeight=allWeight/dogs.length;
System.out.println("总体重="+allWeight+ "平均"+avgWeight);
}
}
class Dog
{
private String name;
private float weight;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public float getWeight(){
return weight;
}
public void setWeight(float weight){
this.name = name;
}
}
------解决方案-------------------- public void setWeight(float weight){
this.name = name;
}
这写错了吧。
另外,你的输入是什么样的啊