Package try_2;
Import java.io.*;
/*
* * Author Han
* Function: Take the hoe for me
*/
public class Demo5_2 {
public static void Main (string[] args) throws Exception
{
Dog[] Dogs=new dog[4];
Enter information for each dog from the console
InputStreamReader isr=new InputStreamReader (system.in);//Receive bytes entered from the console
This is similar to Scanner in=new Scanner (system.in)
Constructs a character stream cache that contains characters converted into bytes that are stored in the console input.
BufferedReader br=new BufferedReader (ISR);
Here, with the sentence. Add a stream of characters to the buffered stream
In fact, the above two sentences can be divided into three sentences or a sentence
/* as follows
* InputStream is = system.in;
InputStreamReader ISR = new InputStreamReader (IS);
BufferedReader bufr = new BufferedReader (ISR);
or a sentence
BufferedReader br=new BufferedReader (New InputStreamReader (system.in)); This is good.
*/
for (int i=0;i<4;i++)
{
Dogs[i]=new Dog ();
System.out.println ("Please enter dog name:");
Reading from the console
String Name=br.readline ();
Pay the name to the object
Dogs[i].setname (name);
System.out.println ("Please enter the weight of the dog:");
String S_weight=br.readline ();
Float weight=float.parsefloat (s_weight);//string->float
Pay the weight to the object
DOGS[I].SETWW (weight);
}
Calculate total weight
float allww=0;
for (int i=0;i<4;i++)
{
ALLWW+=DOGS[I].GETWW ();
}
Calculate Average weight
float avgww=allww/dogs.length;
SYSTEM.OUT.PRINTLN ("Total weight =" +allww+ "average =" +avgww);
}
}
Define a Dog class
Class Dog
{
private String name;
private float weight;
Public String GetName ()
{return name;}
public void SetName (String name) {
This.name=name;
}
public float GETWW ()
{
return weight;
}
public void Setww (float weight)
{
This.weight=weight;
}
}
Yard farmer Open Rich peasant, hoe must take first