자바 가 쓴 studentlist 에 대한 간단 한 클래스

46598 단어 자바
emm 는 처음으로 자 바 를 썼 습 니 다.그 동안 많은 자 료 를 찾 았 습 니 다.그리고 다른 사람의 글 씨 를 참고 하 였 습 니 다.쓰 고 빨리 acm 하 러 갔 어 요.
import java.util.Scanner;

import	java.io.*;

import java.text.DecimalFormat;

class student{
			private String snumber;
			private String sname;
			private int markfm;
			private int markfe;
			private int markfs;
			student(String snumber,String sname)
			{
				this.snumber=snumber;
				this.sname=sname;
				
			}
			String getname()
			{
				return sname;
			}
			String getnumber()
			{
				return snumber;
			}
			void entermarks(int markfm,int markfe,int markfs)
			{
				this.markfe=markfe;
				this.markfm=markfm;
				this.markfs=markfs;
			}
			int getm()
			{
				return markfm;
			}
			int gete()
			{
				return markfe;
			}
			int gets(){
				return markfs;
			}
			double calav()
			{
				return (float)(markfe+markfm+markfs)/3;
			}
			public String toString() {
					return ("Number:"+this.snumber+"
Name:"
+this.sname+"
Math:"
+this.markfm+"
English:"
+this.markfe+"
Science:"
+this.markfs+"
Ave:"
+String.format("%.2f",calav())); }} class StudentList{ student[] list; int total=0; StudentList(int lenth) { list=new student[lenth]; } boolean add(student stu) { if(total>=list.length) { return false; } else { list[total]=new student(stu.getnumber(),stu.getname()); list[total++].entermarks(stu.getm(), stu.gete(), stu.gets()); return true; } } boolean remove(int no) { if(no>=total) { return false; } else { for(int j=no;j<total-1;j++) { list[j]=list[j+1]; } total--; return true; } } boolean remove(String number) { if(!isEmpty()) { for(int i=0;i<total;i++) { if(list[i].getnumber()==number) { for(int j=i;j<total-1;j++) { list[j]=list[j+1]; } total--; return true; } }} return false; } boolean isEmpty() { if(total==0)return true; return false; } student getitem(int no) { if(no>=total)return null; //no--; return list[no]; } student getitem(String number) { for(int i=0;i<total;i++) { if(list[i].getnumber()==number) { return list[i]; } } return null; } int gettotal() { return total; } } public class students { public static void main(String[] args) { StudentList list1=new StudentList(500); String line; Scanner ss=new Scanner(System.in); int markForMaths; int markForEnglish; int markForScience; String studentNumber; String studentName; while(ss.hasNext()) { line=ss.nextLine().trim(); String[] s1=line.split(" "); String s2=s1[0].trim(); if(Integer.valueOf(s2).intValue()==1) { studentNumber =s1[1]; studentName = s1[2]; markForMaths =Integer.valueOf(s1[3]).intValue(); markForEnglish=Integer.valueOf(s1[4]).intValue(); markForScience=Integer.valueOf(s1[5]).intValue(); student stu = new student(studentNumber,studentName); stu.entermarks(markForMaths, markForEnglish, markForScience); if(list1.add(stu)) { System.out.println(list1.gettotal()+" Students at the moment as described below:"); for(int i=0;i<list1.gettotal();i++) System.out.println(list1.getitem(i).toString()); } else {System.out.println(" :");} } if(Integer.valueOf(s2).intValue()==2) { studentNumber=s1[1]; if(list1.remove(studentNumber)) { //syso System.out.println("Remove successfully"); System.out.println(list1.total+" Students at the moment as described below:"); for(int i=0;i<list1.total;i++) { System.out.println(list1.getitem(i).toString()); } } else { System.out.println("No such student"); } } if(Integer.valueOf(s2).intValue()==3) { int temp=Integer.valueOf(s1[1]).intValue(); if(list1.remove(temp-1)) { //syso System.out.println("Remove successfully"); System.out.println(list1.total+" Students at the moment as described below:"); for(int i=0;i<list1.total;i++) { System.out.println(list1.getitem(i).toString()); } } else { System.out.println("No such student"); } } if(Integer.valueOf(s2).intValue()==4) { if(list1.isEmpty()) { System.out.println(" "); } else { { System.out.println(" "); } } } if(Integer.valueOf(s2).intValue()==5) { int temp=Integer.valueOf(s1[1]).intValue(); if(list1.getitem(temp-1)!=null) { System.out.println("Student Info:
"
+list1.getitem(temp-1).toString()); } else System.out.println("No such student"); } if(Integer.valueOf(s2).intValue()==6) { int temp=Integer.valueOf(s1[1]).intValue(); if(list1.getitem(s1[1])!=null) System.out.println("Student Info:
"
+list1.getitem(s1[1]).toString()); else System.out.println("No such student"); } if(Integer.valueOf(s2).intValue()==7) { System.out.println(list1.total+" Students at the moment as described below:"); for(int i=0;i<list1.gettotal();i++) { System.out.println(list1.getitem(i).toString()); } } if(Integer.valueOf(s2).intValue()==8) { System.exit(0); } } } }

좋은 웹페이지 즐겨찾기