代码如下:
public class Test {public static void main(String[] args) {
String[] nameArr = new String[5]
// 赋值4个
nameArr[0] = "name0"
nameArr[1] = "name1"
nameArr[2] = "name2"
nameArr[3] = "name3"
// 再添加一个
nameArr[4] = "最后添加的"
// 遍历数组,分别取到数组中的元素
for (int i = 0 i < nameArr.length i++) {
System.out.print(nameArr[i] + " ")
}
}
}
运行效果:
import java.util.Scannerpublic class 个人信息
{
public static void main(String[] args)
{
System.out.println("\n\t\t==========简单的个人信息录入==========\n")
init()
}//初始化!
private static void init()
{
/*int a1 = 073
long b1 = 0xa38f
System.out.println(a1+"....."+b1)*/
for ( )
{
System.out.println("请输入姓名:")
String a=Input.getName()
System.out.println("请输入年龄:")
int b=Input.getAge()
System.out.println("请输入学历:")
String c=Input.getEducation()
System.out.println("你输入的信息^_^\t名字:"+a+" 年龄:"+b+" 学历:"+c+"\n")
}
}
}
class Input
{
private static Scanner sc=new Scanner(System.in)
private Input(){}
static String getName()
{
String name=sc.next()
return name
}
static int getAge()
{
int age=sc.nextInt()
return age
}
static String getEducation()
{
String education=sc.next()
return education
}
}
首先在任一目录保存以下代码
编译代码:javac PersonInfo.class
执行代码:java PersonInfo
代码输出效果如下:
id = 001, name = 张三, sex = 男, 年龄 = 12, 身份z = 140421198809021102
id = 002, name = 李四, sex = 女, 年龄 = 25, 身份z = 280421198809022103
id = 003, name = 王五, sex = 男, 年龄 = 33, 身份z = 355421198809021104
id = 004, name = 赵六, sex = 女, 年龄 = 17, 身份z = 472421198809021105
import java.util.ArrayList
import java.util.List
public class PersonInfo{
private String id//id
private String name//姓名
private int sex//性别
private int age//年龄
private String idCard//身份z
@Override
public String toString() {
return "id = " + getId() + ", name = "
+ getName() + ", sex = " + (getSex()==0?"男":"女")
+ ", 年龄 = " + getAge() + ", 身份z = " + getIdCard()
}
public PersonInfo(String id, String name, int sex, int age, String idCard) {
this.id = id
this.name = name
this.sex = sex
this.age = age
this.idCard = idCard
}
public static void main(String[] args) {
PersonInfo p1 = new PersonInfo("001", "张三", 0, 12, "140421198809021102")
PersonInfo p2 = new PersonInfo("002", "李四", 1, 25, "280421198809022103")
PersonInfo p3 = new PersonInfo("003", "王五", 0, 33, "355421198809021104")
PersonInfo p4 = new PersonInfo("004", "赵六", 1, 17, "472421198809021105")
List<PersonInfo>personList = new ArrayList<PersonInfo>()
personList.add(p1)
personList.add(p2)
personList.add(p3)
personList.add(p4)
for(PersonInfo p : personList){
System.out.println(p)
}
}
public String getId() {
return id
}
public void setId(String id) {
this.id = id
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public int getSex() {
return sex
}
public void setSex(int sex) {
this.sex = sex
}
public int getAge() {
return age
}
public void setAge(int age) {
this.age = age
}
public String getIdCard() {
return idCard
}
public void setIdCard(String idCard) {
this.idCard = idCard
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)