Java.作业2 myClass

要求:



代码:

import java.util.*;
public class myClass
{
	public static void main(String[] args) 
	{	
		String students[] = new String[3];
		Student Student1 = new Student("20140101",true);
		Student Student2 = new Student("20140102",false);
		Student Student3 = new Student("20140103",false);
		int i = 0;
		System.out.println("请输入姓名:");
		while(i < 3)
		{
			if(i == 0)
			{
				Student1.input();
				students[i] = Student1.name;
				i++;
			}
			if(i == 1)
			{
				Student2.input();
				students[i] = Student2.name;
				i++;
			}
			if(i == 2)
			{
				Student3.input();
				students[i] = Student3.name;
				i++;
			}
			
		}
		
		Student t = new Student();
		if(Student1.name.compareTo(Student2.name) > 0)
		{
			t = Student1;
			Student1 = Student2;
			Student2 = t;
		}
		if(Student2.name.compareTo(Student3.name) > 0)
		{
			t = Student2;
			Student2 = Student3;
			Student3 = t;
		}
		if(Student1.name.compareTo(Student2.name) > 0)
		{
			t = Student1;
			Student1 = Student2;
			Student2 = t;
		}
		System.out.println("根据姓名排序后:");
		Student1.out();
		Student2.out();
		Student3.out();
	}
}

class Student 
{
	String ID;
	String name;
	boolean sex;
	Student()
	{
	}
	Student(String ID,boolean sex)
	{
		this.ID = ID;
		this.sex = sex;
	}
	void input()
	{
		Scanner reader = new Scanner(System.in);
		this.name = reader.next();
	}
	void out()
	{
		System.out.print("姓名:"+name+" 学号:"+ID+" 性别:");
		if(sex == true)
			System.out.println("男");
		else
			System.out.println("女");
	}
}

class Teacher
{
	String name;
	void input()
	{
		Scanner reader = new Scanner(System.in);
		this.name = reader.next();
	}
}

结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值