Description of Requirement
The figure needs to achieve, according to three people entered information, respectively, to calculate the average of three customers and maximum ages
Involving knowledge points:
① constructor
② string cutting
③ array of objects
Code
FIG code structure:
package cn.test.logan.day03; public class Person { String custNo; String custName; int age; String addr; // define a constructor method: execution logic to initialize an object constructing public the Person (Custno String, String custName, int Age, String addr) { the this .custNo = Custno; the this .custName = custName; the this .age = Age; the this .addr = addr; } }
package cn.test.logan.day03; import java.util.Scanner; public class PersonTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // input data System.out.println ( "Please enter personal information:" ); String row1 = sc.nextLine(); String[] split1 = row1.split(","); System.out.println ( "Please enter the second personal information:" ); String row2 = sc.nextLine(); String[] split2 = row2.split(","); System.out.println ( "Please enter personal information:" ); String row3 = sc.nextLine(); String[] split3 = row3.split(","); // constructor three objects the Person P1 = new new the Person (Split1 [0], Split1 [. 1], the Integer.parseInt (Split1 [2]), Split1 [. 3 ]); Person p2 = new Person(split2[0],split2[1],Integer.parseInt(split2[2]),split2[3]); Person p3 = new Person(split3[0],split3[1],Integer.parseInt(split3[2]),split3[3]); // determine the average age of int SUM = p1.age p2.age + + p3.age; a float AVG = ( a float ) SUM /. 3 ; // define an array of objects the Person [] PS = new new the Person [] {P1, P2, P3}; int tmp = ps[0].age; for(int i=1;i<3;i++) { if(ps[i].age > tmp ) { tmp = ps[i].age; } } System.out.println ( "The average age was:" + AVG); System.out.println ( "maximum age:" + tmp); } }