6-1 JAVA成绩比较 (60 分)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44547670/article/details/102755618

6-1 JAVA成绩比较 (60 分)

本题要求实现Student类,该类实现Comparable接口,用于计算两个同学的JAVA成绩差,其中一个同学的数据已经输入,只需要从键盘输入第二个同学的信息(只有姓名和JAVA成绩两项),最终返回成绩差。

裁判测试程序样例:

在这里给出该类被调用进行测试的例子。例如:

import java.util.Scanner;

interface Comparable {    						
	int compareTo(Student  student);
}


public class Main {
	public static void main(String[] args) {				
		Scanner input = new Scanner(System.in);
		Student p1=new Student("张三", 88);
		String name = input.next();
		int score = input.nextInt();
		Student p2 = new Student(name,score);
		System.out.println(p1.compareTo(p2));
		input.close();
	}

}

/* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

chen 60

输出样例:

在这里给出相应的输出。例如:

28

解答

class Student implements Comparable{
	int score;
	String name;
	
	Student(String name, int score){
		this.name = name;
		this.score = score;
	}
	
	public int compareTo(Student student) {
		return score - student.score;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44547670/article/details/102755618