麻烦Java大神注释一下这个代码~~万分感谢!! 10
因为是新手菜鸟,老师只给了代码没讲不太懂。所以麻烦大神给注释解释一下。谢谢谢谢!importjava.util.Scanner;publicclassTestClass{...
因为是新手菜鸟,老师只给了代码没讲不太懂。所以麻烦大神给注释解释一下。谢谢谢谢!
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
ECStu14 e=new ECStu14();
ECStu14[] ecss;
ecss=new ECStu14[2];
for(int i=0;i<ecss.length;i++){
ecss[i]=new ECStu14();
}
e.inputData(ecss);
e.outputData(ecss);
e.sortData(ecss);
e.outputData(ecss);
}
}
class ECStu14{
int stuNo;
String stuName;
double math;
double english;
double sum;
void inputData(ECStu14[] ecs){
Scanner reader= new Scanner(System.in);
for(int i=0;i<ecs.length;i++){
ecs[i].stuNo=reader.nextInt();
ecs[i].stuName=reader.next();
ecs[i].math=reader.nextDouble();
ecs[i].english=reader.nextDouble();
ecs[i].sum=ecs[i].math+ecs[i].english;
}
}
void outputData(ECStu14[] ecs){
System.out.println(" 学号 | 姓名 | 数学 |英语 |总分");
for(int i=0;i<ecs.length;i++){
System.out.print(ecs[i].stuNo+"|");
System.out.print(ecs[i].stuName+"|");
System.out.print(ecs[i].math+"|");
System.out.print(ecs[i].english+"|");
System.out.println(ecs[i].sum);
}
}
void sortData(ECStu14[]ecs){
for(int i=0;i<ecs.length;i++){
for(int j=0;j<ecs.length-i-1;j++){
if(ecs[j].sum>ecs[j+1].sum){
ECStu14 ec=new ECStu14();
ec=ecs[j];
ecs[j]=ecs[j+1];
ecs[j+1]=ec;
}
}
}
}
} 展开
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
ECStu14 e=new ECStu14();
ECStu14[] ecss;
ecss=new ECStu14[2];
for(int i=0;i<ecss.length;i++){
ecss[i]=new ECStu14();
}
e.inputData(ecss);
e.outputData(ecss);
e.sortData(ecss);
e.outputData(ecss);
}
}
class ECStu14{
int stuNo;
String stuName;
double math;
double english;
double sum;
void inputData(ECStu14[] ecs){
Scanner reader= new Scanner(System.in);
for(int i=0;i<ecs.length;i++){
ecs[i].stuNo=reader.nextInt();
ecs[i].stuName=reader.next();
ecs[i].math=reader.nextDouble();
ecs[i].english=reader.nextDouble();
ecs[i].sum=ecs[i].math+ecs[i].english;
}
}
void outputData(ECStu14[] ecs){
System.out.println(" 学号 | 姓名 | 数学 |英语 |总分");
for(int i=0;i<ecs.length;i++){
System.out.print(ecs[i].stuNo+"|");
System.out.print(ecs[i].stuName+"|");
System.out.print(ecs[i].math+"|");
System.out.print(ecs[i].english+"|");
System.out.println(ecs[i].sum);
}
}
void sortData(ECStu14[]ecs){
for(int i=0;i<ecs.length;i++){
for(int j=0;j<ecs.length-i-1;j++){
if(ecs[j].sum>ecs[j+1].sum){
ECStu14 ec=new ECStu14();
ec=ecs[j];
ecs[j]=ecs[j+1];
ecs[j+1]=ec;
}
}
}
}
} 展开
展开全部
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
// 定义ECStu4类的对象e
ECStu14 e = new ECStu14();
// 定义数组ecss,类型为ECStu14
ECStu14[] ecss;
ecss = new ECStu14[2];
// 为数组创建2个元素,元素为ECstu14类的对象
for (int i = 0; i < ecss.length; i++) {
ecss[i] = new ECStu14();
}
// 输入数据
e.inputData(ecss);
// 输出数据
e.outputData(ecss);
// 排序
e.sortData(ecss);
// 输出排序后的数据
e.outputData(ecss);
}
}
// 定义类ECStu14
class ECStu14 {
// 5个成员变量
int stuNo;
String stuName;
double math;
double english;
double sum;
// 输入数据方法,依次输入2个学生的学号,姓名,数学成绩,英语成绩.总分根据前面输入的成绩自动计算
void inputData(ECStu14[] ecs) {
Scanner reader = new Scanner(System.in);
for (int i = 0; i < ecs.length; i++) {
ecs[i].stuNo = reader.nextInt();
ecs[i].stuName = reader.next();
ecs[i].math = reader.nextDouble();
ecs[i].english = reader.nextDouble();
ecs[i].sum = ecs[i].math + ecs[i].english;
}
}
// 输出两个学生的信息
void outputData(ECStu14[] ecs) {
System.out.println(" 学号 | 姓名 | 数学 |英语 |总分");
for (int i = 0; i < ecs.length; i++) {
System.out.print(ecs[i].stuNo + "|");
System.out.print(ecs[i].stuName + "|");
System.out.print(ecs[i].math + "|");
System.out.print(ecs[i].english + "|");
System.out.println(ecs[i].sum);
}
}
// 根据总分对两个学生进行排序
void sortData(ECStu14[] ecs) {
for (int i = 0; i < ecs.length; i++) {
for (int j = 0; j < ecs.length - i - 1; j++) {
if (ecs[j].sum > ecs[j + 1].sum) {
ECStu14 ec = new ECStu14();
ec = ecs[j];
ecs[j] = ecs[j + 1];
ecs[j + 1] = ec;
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询