程序一:数据类型、运算符、表达式的使用
public class operation_test
{
int num1=25,num2=3;//参与测试的数字
boolean a;//布尔型变量
int b;//整型
float c;//单精度浮点型
char d='x';//字符型变量
void operation_test ()//定义函数测试各种运算符和数据类型
{
System.out.println("数据类型和运算符测试");
a=num1<num2;
System.out.println("不等号运算&布尔变量测试判断num1<num2:"+a);//测试布尔型变量的false
a=num1>num2;
System.out.println("不等号运算&布尔变量测试判断num1>num2:"+a);//测试布尔型变量的true
b=num1+num2;//num1+num2
System.out.println("加法运算&整型测试:"+b);//测试整型
c=num1*num2;//num1*num2
System.out.println("乘法运算&浮点型测试:"+c);//测试浮点型
System.out.println("字符型数据测试:"+d);//测试字符型
}
public static void main(String args[])//java application程序主入口函数,带命令行参数
{
operation_test test1;//定义类的成员
test1=new operation_test();//创建新成员
test1.operation_test();//调用运算测试函数
}
}
运行结果:
程序二:基本控制语句:判断、分支、循环等语句的使用
public class control_test
{ int num1=25;
int num2=3;//两个用于测试的变量
int i;//循环次数用的变量
void control_test()//测试if、while、for语句的使用
{
System.out.println("逻辑控制语句测试");
if(num1>num2)
System.out.println("判断语句if测试:num1>num2");
while(num1<30)//用while循环使num1自增
{
num1++;
System.out.println("循环语句while测试:num1此时的数值是:"+num1);
}
for(i=1;i<=10;i++)//用while循环使num2自增
{
num2++;
System.out.println("循环语句for测试:num2此时的数值是:"+num2);
}
}
public static void main(String args[])//java application程序主入口函数,带命令行参数
{
control_test test1;//定义类的成员
test1=new control_test();//创建新成员
test1.control_test();//调用控制测试语句
}
}
运行结果:
程序三:数组的使用
public class array_test {
void array_test()//数组测试函数
{
int j;//定义一个变量用于数组元素的标号
int[] A;//声明一个数组
A=new int[5];//分配空间,元素数为5
System.out.println("数组赋值测试");
for(j=0;j<5;j++)//对数组元素进行赋值
A[j]=j;
for(j=0;j<5;j++)//输出数组元素
System.out.println("数组测试:"+"A["+j+"]="+A[j]);
}
public static void main(String args[])//java application程序主入口函数,带命令行参数
{
array_test test1;//定义类的成员
test1=new array_test();//创建新成员
test1.array_test();//调用数组测试语句
}
}
运行结果: