[java]代码库
package com.text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Text1 {
public static void main(String[] args) throws IOException {
Dog dogs[]= new Dog[4];
//控制台输入狗的名字和体重
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
for (int i = 0; i < dogs.length; 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 sum=0;
for (int i = 0; i < dogs.length; i++) {
sum+=dogs[i].getWeight();
}
System.out.println("总体重为"+sum+" 平均体重为"+(sum/dogs.length
));
//找出体重最大的狗
float maxWeight =dogs[0].getWeight();
int indext=0;
for (int i = 1; i < dogs.length; i++) {
if (maxWeight<dogs[i].getWeight()) {
maxWeight=dogs[i].getWeight();
indext=i;
}
}
System.out.println("最大体重狗是"+dogs[indext].getName()+" 体重为"+maxWeight);
}
}
//定义狗类
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.weight = weight;
}
}
中级程序员
by: 晚起的鸟儿 发表于:2016-09-30 15:48:07 顶(0) | 踩(0) 回复
回复评论