1、Java编程实验public class HelloWorld public static void main(String args) System.out.println(Hello world!); /定义一个分数,当60是public class IFELSEstatement public static void main(String args) int score = 65; if (score=60) System.out.println(考试及格了!); else System.out.println(考试不及格,需要补考!); public class CONTINUEst
2、atement public static void main(String args) int i=1; for(i=1;i=10;i+) if(i%3=0)/如果i能被3整除,则执行下一次循环,循环体下面剩余的语句无法执行 continue; System.out.println(i = + i); public class DOWHILEstatement public static void main(String args) int sum=0,i=1; do sum = sum + i; /首先进行sum+i的操作,然后把二者之和赋给变量sum i = i +1; /循环变量的值加
3、1 while(i=100); /当i0)&(i0并且ix2) System.out.println(x1 x2); else if(x1x2) System.out.println(x1 x2); else System.out.println(x1 = x2); public static void main(String args) test(10,5); test(5,10); test(5,5); public class BREAKstatement public static void main(String args) int i=1; for(i=1;i20;i+) if(i
4、=15) /如果 i =15,则跳出循环体 break; System.out.println(i= + i); /打印 i 值 public class ArrayExample0 public static void main(String args) double a1 ; a1=new double8; /为a1分配8个double型元素的存储空间 char a2 =A,B,C,D,E,F,H,I; /直接指定初值方式 /为数组 a1 中的每一个元素赋值 for(int i=0;ia1.length;i+) a1i=100.0+i; /下面各句测定各数组的长度 System.out.p
5、rintln(a1.length=+a1.length); System.out.println(a2.length=+a2.length); public class FORstatement public static void main(String args) int sum=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); /打印计算结果 public class AutoInc public static
6、 void main(String args) int i = 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.printl
7、n(i : + i); public class ArrayExample3 public static void main(String args) int i,j; i = 0; j = 0; int ss = 1,2,3,10,20,30,100,200,300; for(i=0;iss.length;i+) for (j=0;jssi.length;j+) System.out.print(ss+i+j+=+ssij+ ); System.out.println(); public class ArrayExample2 public static void main(String a
8、rgs) int i=1; int yh = new int8; for(i=0;i0;j-) yhj= yhj-1+yhj; for (int j=0;j=i;j+) System.out.print(yhj+t); System.out.println(); public class ArrayExample1 public static void main(String args) int i=1; int aa = new int5; for(i=0;ib) t=a; a=b; b=t; System.out.println(a=+a+,b=+b); public class WHIL
9、Estatement public static void main(String args) int sum=0,i=1; while(i=100) /判断 i 是否 =100,如果是,则执行循环体 sum = sum + i; i = i +1; /循环变量加1,防止死循环 System.out.println(sum = + sum); public class MULFORstatement public static void main(String args) int i,j; for(i=1;i=9;i+)/第一重循环 for(j=1;j= 10); long l = -1; S
10、ystem.out.println(l = 10); short s = -1; System.out.println(s = 10); byte b = -1; System.out.println(b = 10); b = -1; System.out.println(b10); class Employee private double employeeSalary = 1800; private String employeeName = 姓名未知。; private int employeeNo; Employee() System.out.println(不带参数的构造函数被调用!
11、); public Employee(String name) employeeName = name; System.out.println(带有姓名参数的构造函数被调用!); / System.out.println(职员的姓名: + name); public Employee(String name,double salary) employeeName = name; employeeSalary = salary; System.out.println(带有姓名和薪水这两个参数的构造函数被调用!); / System.out.println(职员的姓名: + name + 薪水:
12、+ employeeSalary); public String toString() /输出员工的基本信息 String s; s = 编号: + employeeNo + 姓名: + employeeName + 工资: + employeeSalary; return s; public class TestEmployee1 public static void main(String args) Employee e1=new Employee(); System.out.println(e1.toString(); /* Employee e2=new Employee(李萍);
13、System.out.println(e2.toString(); Employee e3=new Employee(王嘉怡,2400); System.out.println(e3.toString();*/ class Employee /定义父类: 职员类 private String employeeName; /职员姓名 private int employeeNo; /职员的编号 private double employeeSalary; / 职员的薪水 public void setEmployeeName(String name)/设置职员的姓名 employeeName =
14、 name; public void setEmployeeNo(int no)/设置职员的编号 employeeNo = no; public void setEmployeeSalary(double salary) /设置职员的薪水 employeeSalary = salary ; public String getEmployeeName() /获取职员姓名 return employeeName; public int getEmployeeNo() /获取职员编号 return employeeNo; public double getEmployeeSalary() /获取职员
15、工资 return employeeSalary ; public String toString() /输出员工的基本信息 String s; s = 编号: + employeeNo + 姓名: + employeeName + 工资: + employeeSalary; return s; class School String name; int num; public String toString() return name+ +num; class Employee1 public Employee1() System.out.println(构造函数被调用!);private
16、double employeeSalary = 1800; public void getEmployeeSalary() System.out.println(职员的基本薪水为: +employeeSalary); public class test_employee public static void main(String args) /Employee的第一个对象employee1 Employee employee1; /声明Employee的对象employee employee1 = new Employee(); /为对象employee分配内存 employee1.setE
17、mployeeName(王一); employee1.setEmployeeNo(100001); employee1.setEmployeeSalary(2100); System.out.println(employee1.toString(); /Employee的第一个对象employee2 Employee employee2; /声明Employee的对象employee employee2 = new Employee(); /为对象employee分配内存 System.out.println(employee2.toString(); import java.io.*;cla
18、ss Employee /定义父类: 职员类 private String employeeName; /职员姓名 private int employeeNo; /职员的编号 private double employeeSalary; / 职员的薪水 public void setEmployeeName(String name)/设置职员的姓名 employeeName = name; public void setEmployeeNo(int no)/设置职员的编号 employeeNo = no; public void setEmployeeSalary(double salary
19、) /设置职员的薪水 employeeSalary = salary ; public String getEmployeeName() /获取职员姓名 return employeeName; public int getEmployeeNo() /获取职员的编号 return employeeNo; public double getEmployeeSalary() /获取职员工资 return employeeSalary ; public String toString() /输出员工的基本信息 String s; s = 编号: + employeeNo + 姓名: + employ
20、eeName + 工资: + employeeSalary; return s; public class test_employee public static void main(String args) /Employee的第一个对象employee1 Employee employee1; /声明Employee的对象employee,也称employee为引用变量 employee1 = new Employee(); /为对象employee分配内存 employee1.setEmployeeName(王一); employee1.setEmployeeNo(100001); em
21、ployee1.setEmployeeSalary(2100); System.out.println(employee1.toString(); /Employee的第二个对象employee2,并为对象employee分配内存 Employee employee2 = new Employee(); / System.out.println(employee2.toString(); /test_commandLine_arguments.javaclass test_commandLine_arguments static int stVar = 12; /这是一个局部变量,只在本块内有
22、效 System.out.println(This is static block. + stVar); public static void main(String args) /依次获取命令行参数并输出 for(int i=0;iargs.length;i+) System.out.println(args+i+: +argsi); class SimpleValue public static void main(String args) int x = 5; change(x); System.out.println(x); public static void change(int
23、x) x = 3; /* test_accessStatic.java*/ class test_accessStatic private static int count; /定义一个静态成员变量,用于统计对象的个数 private String name; /定义一个非静态的成员变量 public test_accessStatic(String Name) name = Name;count+;/定义一个静态方法,测试静态的方法是否能够调用非静态的数据成员和方法 public static void accessStaticMethod() int i = 0; /正确,可以有自己的局部
24、变量 count+; /正确,静态方法可以使用静态变量 anotherStaticMethod(); /正确,可以调用静态方法 / name = 静态对象; /错误,不能使用实例变量 / toString(); /错误,不能调用实例方法 public static void anotherStaticMethod() /类中另一个静态的方法 System.out.println(测试能被类中静态和非静态方法调用的静态方法); count+; /下面定义一个实例方法 public void resultMethod() anotherStaticMethod(); /正确,可以调用静态方法 System.out.println(新建对象的信息 + name );
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1