Java编程实验.docx
- 文档编号:24050602
- 上传时间:2023-05-23
- 格式:DOCX
- 页数:18
- 大小:17.99KB
Java编程实验.docx
《Java编程实验.docx》由会员分享,可在线阅读,更多相关《Java编程实验.docx(18页珍藏版)》请在冰豆网上搜索。
Java编程实验
publicclassHelloWorld{
publicstaticvoidmain(String[]args){
System.out.println("Helloworld!
");
}
}
//定义一个分数,当>60是
publicclassIFELSEstatement{
publicstaticvoidmain(String[]args){
intscore=65;
if(score>=60){
System.out.println("考试及格了!
");
}else{
System.out.println("考试不及格,需要补考!
");
}
}
publicclassCONTINUEstatement{
publicstaticvoidmain(String[]args){
inti=1;
for(i=1;i<=10;i++)
{
if(i%3==0)
//如果i能被3整除,则执行下一次循环,循环体下面剩余的语句无法执行
continue;
System.out.println("i="+i);
}
}
}
publicclassDOWHILEstatement{
publicstaticvoidmain(String[]args){
intsum=0,i=1;
do
{
sum=sum+i;//首先进行sum+i的操作,然后把二者之和赋给变量sum
i=i+1;//循环变量的值加1
}while(i<=100);//当i<=100时,继续执行循环体
System.out.println("sum="+sum);//打印计算结果
}
}
publicclassCLoperation{
publicstaticvoidmain(String[]args){
inti=1;
booleanb1=((i>0)&&(i<100));//由于i>0并且i<100,所以b1的值为true
System.out.println(b1);
}
}
publicclassIfElseNest{
staticintresult=0;
staticvoidtest(intx1,intx2){
if(x1>x2){
System.out.println("x1>x2");
}else{
if(x1 System.out.println("x1 }else{ System.out.println("x1=x2"); } } } publicstaticvoidmain(String[]args){ test(10,5); test(5,10); test(5,5); } } publicclassBREAKstatement{ publicstaticvoidmain(String[]args){ inti=1; for(i=1;i<20;i++){ if(i==15)//如果i=15,则跳出循环体 break; } System.out.println("i="+i);//打印i值 } } publicclassArrayExample0{ publicstaticvoidmain(String[]args){ doublea1[]; a1=newdouble[8];//为a1分配8个double型元素的存储空间 chara2[]={'A','B','C','D','E','F','H','I'};//直接指定初值方式 //为数组a1中的每一个元素赋值 for(inti=0;i a1[i]=100.0+i; } //下面各句测定各数组的长度 System.out.println("a1.length="+a1.length); System.out.println("a2.length="+a2.length); } } publicclassFORstatement{ publicstaticvoidmain(String[]args){ intsum=0,i=1;//为sum和i赋初值 for(i=1;i<=100;i++){ if(i%3==0)//判断i能否整除3 { sum=sum+i; } } System.out.println("sum="+sum);//打印计算结果 } } publicclassAutoInc{ publicstaticvoidmain(String[]args){ inti=1; System.out.println("i: "+i); System.out.println("++i: "+++i);//Pre-increment System.out.println("i++: "+i++);//Post-increment System.out.println("i: "+i); System.out.println("--i: "+--i);//Pre-decrement System.out.println("i--: "+i--);//Post-decrement System.out.println("i: "+i); } } publicclassArrayExample3{ publicstaticvoidmain(String[]args){ inti,j; i=0; j=0; intss[][]={{1,2,3},{10,20,30},{100,200,300}}; for(i=0;i { for(j=0;j { System.out.print("ss["+i+"]["+j+"]="+ss[i][j]+""); } System.out.println(); } } } publicclassArrayExample2{ publicstaticvoidmain(String[]args){ inti=1; intyh[]=newint[8]; for(i=0;i<8;i++){ yh[i]=1; for(intj=i-1;j>0;j--){ yh[j]=yh[j-1]+yh[j]; } for(intj=0;j<=i;j++){ System.out.print(yh[j]+"\t"); } System.out.println(); } } } publicclassArrayExample1{ publicstaticvoidmain(String[]args){ inti=1; intaa[]=newint[5]; for(i=0;i<5;i++) {aa[i]=2*i+1; System.out.println("aa[i]="+aa[i]); } } } publicclassIFstatement{ publicstaticvoidmain(String[]args){ inta=2,b=1,t; if(a>b){ t=a; a=b; b=t; } System.out.println("a="+a+",b="+b); } } publicclassWHILEstatement{ publicstaticvoidmain(String[]args){ intsum=0,i=1; while(i<=100)//判断i是否<=100,如果是,则执行循环体 { sum=sum+i; i=i+1;//循环变量加1,防止死循环 } System.out.println("sum="+sum); } } publicclassMULFORstatement{ publicstaticvoidmain(String[]args){ inti,j; for(i=1;i<=9;i++)//第一重循环 { for(j=1;j<=i;j++)//第二重循环 { System.out.print(i+"*"+j+"="+(i*j)+"\t"); } System.out.println();//换行 } } } publicclassSWITCHstatement{ publicstaticvoidmain(String[]args){ intx=6,y=9; charop='+'; //根据变量op的值执行相应的运算 switch(op) { case'+': //输出x+y System.out.println(x+y); break; case'-': //输出x-y System.out.println(x-y); break; case'*': //输出x*y System.out.println(x*y); break; case'/': //输出x/y System.out.println(x/y); break; default: System.out.println("没有找到合适的运算符! "); } } } publicclassURshift{ publicstaticvoidmain(String[]args){ inti=-1; System.out.println(i>>>=10); longl=-1; System.out.println(l>>>=10); shorts=-1; System.out.println(s>>>=10); byteb=-1; System.out.println(b>>>=10); b=-1; System.out.println(b>>>10); } } classEmployee{ privatedoubleemployeeSalary=1800; privateStringemployeeName="姓名未知。 "; privateintemployeeNo; Employee(){ System.out.println("不带参数的构造函数被调用! "); } publicEmployee(Stringname){ employeeName=name; System.out.println("带有姓名参数的构造函数被调用! "); //System.out.println("职员的姓名: "+name); } publicEmployee(Stringname,doublesalary){ employeeName=name; employeeSalary=salary; System.out.println("带有姓名和薪水这两个参数的构造函数被调用! "); //System.out.println("职员的姓名: "+name+"薪水: "+employeeSalary); } publicStringtoString(){//输出员工的基本信息 Strings; s="编号: "+employeeNo+"姓名: "+employeeName +"工资: "+employeeSalary; returns; } } publicclassTestEmployee1{ publicstaticvoidmain(String[]args){ Employeee1=newEmployee(); System.out.println(e1.toString()); /*Employeee2=newEmployee("李萍"); System.out.println(e2.toString()); Employeee3=newEmployee("王嘉怡",2400); System.out.println(e3.toString());*/ } } classEmployee{//定义父类: 职员类 privateStringemployeeName;//职员姓名 privateintemployeeNo;//职员的编号 privatedoubleemployeeSalary;//职员的薪水 publicvoidsetEmployeeName(Stringname){//设置职员的姓名 employeeName=name; } publicvoidsetEmployeeNo(intno){//设置职员的编号 employeeNo=no; } publicvoidsetEmployeeSalary(doublesalary){//设置职员的薪水 employeeSalary=salary; } publicStringgetEmployeeName(){//获取职员姓名 returnemployeeName; } publicintgetEmployeeNo(){//获取职员编号 returnemployeeNo; } publicdoublegetEmployeeSalary(){//获取职员工资 returnemployeeSalary; } publicStringtoString(){//输出员工的基本信息 Strings; s="编号: "+employeeNo+"姓名: "+employeeName +"工资: "+employeeSalary; returns; } } classSchool { Stringname; intnum; publicStringtoString() {returnname+""+num;} } classEmployee1{ publicEmployee1() { System.out.println("构造函数被调用! "); } privatedoubleemployeeSalary=1800; publicvoidgetEmployeeSalary() { System.out.println("职员的基本薪水为: "+employeeSalary); } } publicclasstest_employee { publicstaticvoidmain(Stringargs[]) {//Employee的第一个对象employee1 Employeeemployee1;//声明Employee的对象employee employee1=newEmployee();//为对象employee分配内存 employee1.setEmployeeName("王一"); employee1.setEmployeeNo(100001); employee1.setEmployeeSalary(2100); System.out.println(employee1.toString()); //Employee的第一个对象employee2 Employeeemployee2;//声明Employee的对象employee employee2=newEmployee();//为对象employee分配内存 System.out.println(employee2.toString()); importjava.io.*; classEmployee{//定义父类: 职员类 privateStringemployeeName;//职员姓名 privateintemployeeNo;//职员的编号 privatedoubleemployeeSalary;//职员的薪水 publicvoidsetEmployeeName(Stringname){//设置职员的姓名 employeeName=name; } publicvoidsetEmployeeNo(intno){//设置职员的编号 employeeNo=no; } publicvoidsetEmployeeSalary(doublesalary){//设置职员的薪水 employeeSalary=salary; } publicStringgetEmployeeName(){//获取职员姓名 returnemployeeName; } publicintgetEmployeeNo(){//获取职员的编号 returnemployeeNo; } publicdoublegetEmployeeSalary(){//获取职员工资 returnemployeeSalary; } publicStringtoString(){//输出员工的基本信息 Strings; s="编号: "+employeeNo+"姓名: "+employeeName +"工资: "+employeeSalary; returns; } } publicclasstest_employee { publicstaticvoidmain(Stringargs[]) {//Employee的第一个对象employee1 Employeeemployee1;//声明Employee的对象employee,也称employee为引用变量 employee1=newEmployee();//为对象employee分配内存 employee1.setEmployeeName("王一"); employee1.setEmployeeNo(100001); employee1.setEmployeeSalary(2100); System.out.println(employee1.toString()); //Employee的第二个对象employee2,并为对象employee分配内存 Employeeemployee2=newEmployee();// System.out.println(employee2.toString()); } } } } //test_commandLine_arguments.java classtest_commandLine_arguments { static { intstVar=12;//这是一个局部变量,只在本块内有效 System.out.println("Thisisstaticblock."+stVar); } publicstaticvoidmain(Stringargs[]) {//依次获取命令行参数并输出 for(inti=0;i System.out.println("args["+i+"]: "+args[i]); } } classSimpleValue{ publicstaticvoidmain(String[]args){ intx=5; change(x); System.out.println(x); } publicstaticvoidchange(intx){ x=3; } } /*************************test_accessStatic.java****************************/ classtest_accessStatic{ privatestaticintcount;//定义一个静态成员变量,用于统计对象的个数 privateStringname;//定义一个非静态的成员变量 publictest_accessStatic(StringName){ name=Name; count++; } //定义一个静态方法,测试静态的方法是否能够调用非静态的数据成员和方法 publicstaticvoidaccessStaticMethod(){ inti=0;//正确,可以有自己的局部变量 count++;//正确,静态方法可以使用静态变量 anotherStaticMethod();//正确,可以调用静态方法 //name="静态对象";//错误,不能使用实例变量 //toString();//错误,不能调用实例方法 } publicstaticvoidanotherStaticMethod()//类中另一个静态的方法 { System.out.println("测试能被类中静态和非静态方法调用的静态方法"); count++; } //下面定义一个实例方法 publicvoidresultMethod(){ anotherStaticMethod();//正确,可以调用静态方法 System.out.println("新建对象的信息"+name);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 编程 实验