zoukankan      html  css  js  c++  java
  • 对类的对象进行操作

    设计要求:

    建立一个学生类(姓名,学号,3门课成绩(英语,数学,语文),总分),类的方法:输入,输出,学生数据,根据总分排序,打印学生名次)

    代码:

    import java.util.Scanner;
    class student_           //学生类的Temp
    {
    	String name;
    	String ID;
    	int math;
    	int EN;
    	int ZN;
    	int sum;
    }
    class student
    {
    	student_ temp=new student_(); 
    	String name[];       //学生姓名
    	String ID[];         //学生学号
    	int math[];           //学生数学成绩
    	int EN[];                //学生英语成绩
    	int ZN[];                 //学生语文成绩
    	int sum[];             //学生总成绩
    	
    	void input(int guy)  //输入学生数据
    	{
    		Scanner reader= new Scanner(System.in);
    		int count=0;
    		name=new String[guy];
    		ID=new String[guy];
    		math=new int[guy];
    		EN = new int[guy];
    		ZN = new int[guy];
    		sum= new int[guy];
    		/*根据学生人数输入每个学生的信息,使用循环分别输入*/
    		while(count!=guy&&guy!=0)
    		{
    			System.out.println("Input datas");
    			System.out.println("Enter the name:");  
    			name[count]=reader.next();              //输入学生名字
    			System.out.printf("Enter the ID:
    ");
    			ID[count]=reader.next();                //输入学生学生号
    			System.out.printf("Enter the math:
    ");
    			math[count]=reader.nextInt();           //输入学生数学成绩
    			System.out.printf("Enter the English:
    ");
    			EN[count]=reader.nextInt();             //输入学生英语成绩
    			System.out.printf("Enter the Chinese:
    ");
    		    ZN[count]=reader.nextInt();             //输入学生语文成绩
    			sum[count]=ZN[count]+EN[count]+math[count]; //根据输入的成绩计算总分
    			count++;
    		}
    	}
    	void output(String name_)                 //根据查询的名字输出数据
    	{
    		int guy=name.length;
    		/*通过循环以及euqalsIgnoreCase()string的方法对学生数据进行检索并打印*/
    		for(int i=0; i<guy; i++)
    		{
    			if(name[i].equalsIgnoreCase(name_)) 
    				{
    				System.out.printf("name: %s ID: %s English: %d Math: %d Chinese: %d sum: %d",
    				           name[i],ID[i],EN[i],math[i],ZN[i],sum[i]);
    				}
    		}
    	}
    	/*使用选择排序将学生信息按照总分最高进行排序,排序后并打印所有学生信息*/
    	void printf_student()
    	{
    		int guy=name.length;
    		if(guy!=0) //检测对象中是否有学生信息
    		{	
    		    for(int i=0; i<guy; i++)
    		        for(int k=i+1; k<guy; k++)
    					/*使用学生临时类对象进行选择排序*/
    		            if(sum[k]>sum[i])
    		                {
    		                       temp.name=name[i];
    		                       temp.ID=ID[i];
    		                       temp.EN=EN[i];
    		                       temp.ZN=ZN[i];
    		                       temp.math=math[i];
    		                       temp.sum=sum[i];
    		                       
    		                       name[i]=name[k];
    		                       ID[i]=ID[k];
    		                       EN[i]=EN[k];
    		                       ZN[i]=ZN[k];
    		                       math[i]=math[k];
    		                       sum[i]=sum[k];
    		                       		                       
    		                       name[k]=temp.name;
    		                       ID[k]=temp.ID;
    		                       EN[k]=temp.EN;
    		                       ZN[k]=temp.ZN;
    		                       math[k]=temp.math;
    		                       sum[k]=temp.sum;		                
    		                }
    						/*排序结束后使用循环打印学生信息*/
    			for(int i=0; i<guy; i++)
    			{
    				System.out.printf("name: %s ID: %s English: %d Math: %d Chinese: %d sum: %d 
    ",
    						           name[i],ID[i],EN[i],math[i],ZN[i],sum[i]);
    			}
    		}
    		else  //如果对象中没有信息,将报错
    			System.out.printf("Wrong! This oubject no data:
    ");
    	}
    	
    };
    
    
    public class class_Test {
    	public static void main(String args[])
    	{
    		int guy=0;
    		String name;
    		student A=new student();
    		Scanner reader=new Scanner(System.in);
    		
    		System.out.printf("How many people are there?
     ");
    		guy=reader.nextInt();  //需要录入学生信息数量
    		A.input(guy);
    		System.out.println("printf All student");
    		A.printf_student();    //调用student的类方法打印所有学生信息
    		System.out.println("Who are you looking for?");
    		name=reader.next();
    		A.output(name);        //调用student的类方法查找学生信息
    		System.out.println("Done!");
    	}
    }


  • 相关阅读:
    找到了2年前的一个微博小号
    Float Equal Problem
    有用的护肤品贴
    最近状态总结
    [Coursera]Machine Learning
    KMP算法(转载)
    [Leetcode] Median of Two Sorted Arrays
    [Algorithms(Princeton)] Week1
    [Algorithms(Princeton)] Week1
    [Leetcode] Binary Tree Maximum Path Sum
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732356.html
Copyright © 2011-2022 走看看