📄 question3.java
字号:
class NotExistedStudentInfoException extends Exception
//customized exception throwable for non-existed student info
{
public String toString()
{
return("Student info not existing...");
}
};
class Process//calculate total score and average score, append as suffix
{
String processInfo(String info)
{
String[] unit=info.split(" ");
int length=unit.length;
int [] score=new int[length-2];
int i,sum=0;
float avg;
for (i=2;i<length;i++)
{
score[i-2]=Integer.parseInt(unit[i]);
sum+=score[i-2];
};
avg=sum/(length-2.0f);
String h=" "+sum+" "+avg;
return(info.concat(h));
}
}
class Q3
{
public static void main(String args[]) //args[0] is student number
{
String [] student={ "[M] 20060423 65 43 49 85","[S] 20030214 85 64 98 75 61 84",
"[G] 20040624 85 64 92 71 64 85 73 92"};
try
{
String id=args[0];//obtain the student number
int indexFound=-1;
int ll=student.length;
int i,location=-1;
char specialChar;
for (i=0;i<ll;i++) if (student[i].indexOf(id)>=0)
{
location=student[i].indexOf(id);
indexFound=i;
break;
};
if (indexFound!=-1) specialChar=student[indexFound].charAt(location+id.length());
else specialChar='A';
if (specialChar!=' ') throw new NotExistedStudentInfoException();
student[indexFound]=new Process().processInfo(student[indexFound]);
String [] cc=student[indexFound].split(" ");
int lll=cc.length;
for (i=0;i<lll-4;i++)
{ int yy=i+1;
System.out.print("c"+yy+" ");
}
System.out.print("Total"+" Average\n");
for (i=0;i<lll-3;i++) System.out.print(cc[i+2]+" ");
System.out.println(" "+cc[i+2]);
}
catch (ArrayIndexOutOfBoundsException ee)
{
System.out.println("input parameter error\n");
}
catch (NotExistedStudentInfoException e)
{
System.out.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -