第二次上机作业

1.编写“人”类及其测试类。属性:姓名、性别、年龄、身份证号码。

package FengZhuang;
import java.util.Scanner;
class Person 
{
     String name;
     char sex;
     int age;
     String IdCard;
    public void printf()
    {
        System.out.print("姓名是:"+name+"   ");
        System.out.print("性别是:"+sex+"   ");
        System.out.print("年龄是:"+age+"   ");
        System.out.println("身份证号码是:"+IdCard);
    }
    public Person(String name,char sex,int age,String IdCard)
    {
        this.name=name;
        this.sex=sex;
        this.age=age;
        this.IdCard=IdCard;
        
    }
}
   public class TestPerson
   {
      public static void main(String[] args) 
        {
            Scanner in= new Scanner(System.in);
          String name;
          char sex;
          int age;
          String IdCard; 
          System.out.println("请输入姓名,性别,年龄,身份证号码:");
          name=in.next();
          sex=in.next().charAt(0);
          age=in.nextInt();
          IdCard=in.next();
          Person object=new Person(name,sex,age,IdCard);
          object.printf();
        }
}

 

posted @ 2019-06-05 15:10  王程程程  阅读(203)  评论(0编辑  收藏  举报