java试验程序Word格式.docx
《java试验程序Word格式.docx》由会员分享,可在线阅读,更多相关《java试验程序Word格式.docx(39页珍藏版)》请在冰豆网上搜索。
![java试验程序Word格式.docx](https://file1.bdocx.com/fileroot1/2022-12/30/fb2c6f50-92d0-430f-ab61-875c853879aa/fb2c6f50-92d0-430f-ab61-875c853879aa1.gif)
publicclassshiyan31{publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
请输入长:
\n"
);
inth=sc.nextInt();
请输入宽:
);
intw=sc.nextInt();
ints=h*w;
intc=2*(h+w);
面积是:
\n周长是:
packagedi2;
importjava.util.Random;
publicclassshiyan32{
Randomrd=newRandom();
inta=rd.nextInt(100)+1;
System.out.println(a);
}
}
三:
packagedi3;
publicclassshiyan33{
inta=rd.nextInt(13)+1;
intq=a;
抽到的牌是:
Scannersc=newScanner(System.in);
do
{System.out.println("
请输入决定还再要牌不要:
Y/N\n"
Stringb=sc.next();
if(b.equals("
N"
))
程序结束。
break;
Y"
{a=rd.nextInt(13)+1;
再次发牌,得到的牌是:
q=q+a;
else{
System.out.println("
输入错误,请重新输入"
}while(q<
21);
if(q>
=21)
要牌次数已超过限制"
试验4:
publicclassshiyan41{
int[]score1=newint[13];
int[]score2=newint[13];
intx=0,y=0,i;
for(i=0,i++;
i<
13;
)
{score1[i]=rd.nextInt();
score2[i]=rd.nextInt();
if(score1[i]>
score2[i]){x++;
if(score1[i]<
score2[i]){y++;
elsebreak;
x的得分是:
+x+"
+"
y的得分是:
+y+"
publicclassshiyan42{
Scannerrd=newScanner(System.in);
inti,j,k;
请输入您要的素数个数:
j=rd.nextInt();
for(i=2;
j+2;
i++){
for(k=2;
k<
=i/2;
k++){
if(i%k==0){j++;
break;
}
if(k>
i/2)
System.out.println(i);
}}}
试验5:
packageexp5;
publicclassCollegeextendsStudent{
Stringmajor;
int[]score=newint[5];
publicCollege(StringSno,StringSname,Stringmajor)
{super(Sno,Sname);
this.major=major;
publicCollege(StringSno,StringSname,intSage,Stringmajor)
{super(Sno,Sname,Sage);
this.major=major;
publicvoidSetscore()
{for(inti=0;
5;
i++)
请输入成绩"
score[i]=sc.nextInt();
publicdoubleGetsum()
{doublesum=score[0]+score[1]+score[2]+score[3]+score[4];
returnsum;
publicdoubleGetavg()
{doubleavg=(score[0]+score[1]+score[2]+score[3]+score[4])/5;
returnavg;
publicclassStudent{
StringSno;
StringSname;
intSage;
publicStudent(StringSno,StringSname)
{this.Sno=Sno;
this.Sname=Sname;
publicStudent(StringSno,StringSname,intSage)
{this(Sno,Sname);
this.Sage=Sage;
publicclassMainClass{
String[]Sname=newString[5];
String[]Sno=newString[5];
String[]major=newString[5];
int[]Sage=newint[5];
double[]sum=newdouble[5];
double[]avg=newdouble[5];
Collegestu=newCollege("
"
"
for(inti=0;
请输入姓名:
Sname[i]=sc.next();
请输入学号:
Sno[i]=sc.next();
请输入年龄:
Sage[i]=sc.nextInt();
请输入专业"
major[i]=sc.next();
stu.Setscore();
sum[i]=stu.Getsum();
avg[i]=stu.Getavg();
姓名\t学号\t年龄\t专业\t总分\t平均分\t"
for(intj=0;
j<
j++)
{System.out.println(Sname[j]+"
\t"
+Sno[j]+"
+Sage[j]+"
+major[j]+"
+sum[j]+"
+avg[j]+"
}}}
试验6:
packageexp6;
publicclassClass{publicStringcname;
privateStringcno;
privateintcscore;
publicTeachertea;
publicClass(){};
publicClass(Stringcname){
ame=cname;
publicClass(Stringcname,Stringcno){ame=cname;
o=cno;
publicvoidsetcname(Stringcname){ame=cname;
publicStringgetcname(){returname;
publicvoidsetcno(Stringcno){o=cno;
publicStringgetcno(){returno;
publicvoidsetcscore(intcscore){this.cscore=cscore;
publicintgetcscore(){returnthis.cscore;
}
publicvoidsettea(){
Scannera=newScanner(System.in);
this.tea=newTeacher();
tea.settname(a.next());
publicTeachergettea()
{returnthis.tea;
}}
importjava.util.*;
publicclassCollegeStudentextendsStudent{
publicStringsmajor;
intn;
privateClasscla[]=newClass[20];
publicintscore[]=newint[20];
publicintsum;
publicintavg;
publicCollegeStudent(Stringsno,Stringsname,Stringsmajor)
{super(sno,sname);
this.smajor=smajor;
};
publicCollegeStudent(Stringsno,Stringsname,Stringsmajor,intage)
this.smajor=smajor;
this.age=age;
};
publicvoidsetclass(intn){
Scannera=newScanner(System.in);
for(inti=0;
n;
{cla[i]=newClass(a.next(),a.next());
publicvoidsetscore(intn){
for(inti=0;
{this.score[i]=a.nextInt();
publicvoidsumavg(intn){
for(intj=0;
{this.sum+=score[j];
this.avg=this.sum/n;
publicclassCollegestudent1extendsStudent{
publicClassclas[]=newClass[20];
publicintsum;
publicCollegestudent1(Stringsno,Stringsname,intage)
{super(sno,sname,age);
publicCollegestudent1(Stringsname,intage,Stringsmajor)
{this.sname=sname;
publicvoidsetsname(Stringsname){this.sname=sname;
publicStringgetsname(){returnthis.sname;
publicvoidsetsmajor(Stringsmajor){
this.smajor=smajor;
publicStringgetsmajor(){
returnthis.smajor;
publicvoidsetclass(){
Scannercc=newScanner(System.in);
for(intj=0;
2;
j++){
this.clas[j]=newClass();
clas[j].setcname(cc.next());
publicStringsno;
publicStringsname;
publicintage;
publicStudent(){};
publicStudent(Stringsno,Stringsname)
{this.sno=sno;
this.sname=sname;
publicStudent(Stringsno,Stringsname,intage)
{this(sno,sname);
this.age=age;
publicclassTeacher{
publicStringtname;
privateStringtsex;
privateinttage;
privateStringtschoolar;
privateStringmajorclass;
publicintcscore;
publicTeacher(){}
publicTeacher(Stringtname){this.tname=tname;
publicTeacher(Stringtname,Stringtsex,inttage,Stringtschoolar)
{this.tname=tname;
this.tsex=tsex;
this.tage=tage;
this.tschoolar=tschoolar;
publicTeacher(Stringtname,Stringtsex,inttage,Stringtschoolar,Stringmajorclass)
{this(tname,tsex,tage,tschoolar);
this.majorclass=majorclass;
publicvoidsettname(Stringtname){this.tname=tname;
publicStringgettname(){returnthis.tname;
publicvoidsettsex(Stringtsex){this.tsex=tsex;
publicStringgettsex(){returnthis.tsex;
publicvoidsettage(inttage){this.tage=tage;
publicintgettage(){returnthis.tage;
publicvoidsettschoolar(Stringtschoolar){this.tschoolar=tschoolar;
publicStringgettschoolar(){returnthis.tschoolar;
publicvoidsetmajorclass(Stringmajorclass){this.majorclass=majorclass}
publicStringgetmajorclass(){
returnthis.majorclass;
publicvoidsetcscore(intcscore){this.cscore=cscore;
publicclassMainScore{
Classcla[]=newClass[20];
System.out.println("
输入课程信息"
课程名"
课号"
老师"
3;
{cla[i]=newClass();
cla[i].setcname(a.next());
cla[i].setcno(a.next());
cla[i].settea();
Teachertea[]=newTeacher[20];
输入老师信息"
Scannerb=newScanner(System.in);
老师名"
成绩"
{tea[j]=newTeacher(b.next());
Collegestudent1stu[]=newCollegestudent1[50];
输入学生信息"
Scannercs=newScanner(System.in);
学号"
姓名"
年龄"
课程名,教师名,成绩"
{intsum=0;
intavg=0;
stu[i]=newCollegestudent1(cs.next(),cs.next(),cs.nextInt());
stu[i].setclass();
for(intm=0;
m<
m++)
{System.out.println("
选课"
+stu[i].clas[m].getcname());
stu[i].clas[m].settea();
stu[i].clas[m].gettea().setcscore(cs.nextInt());
sum+=stu[i].clas[m].tea.getcscore();
avg=sum/2;
输出学生总成绩和平均成绩"
);
System.out.println("
总成绩"
+sum+"
平均成绩"
+avg);
}}}
试验7:
packageexp7;
importjava.util.InputMismatchException;
importjava.util.Vector;
publicabstractclassAccountimplementsBankService{
privateintid;
privateStringname;
privateintbalance;
privateStringpassword;
privateVector<
History>
history=newVector<
(100,10);
publicAccount(Stringname,Stringpwd){
Randomrd=newRandom();
this.id=rd.nextInt(10000);
this.name=name;
this.password=pwd;
publicAccount(Stringname,Stringpwd,intbalance){
this(name,pwd);
this.id=rd.nextInt();
this.balance=balance;
publicintgetId(){returnid;
publicvoidsetName(Stringname){this.name=name;
publicStringgetName(){returnname;
//存款
publicintsetBalance(intamount){returnbalance+=amount;
//修改余额
publicintsetBalance1(inta