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();
	}
}

結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章