一、基础实验内容
1、写出程序运行的结果
public class Variable {
public static void main(String[] args) {
int a=10;
System.out.println(“a=”+a);
}
}
运行结果:a=10
2、修改上述程序,定义一个字符型变量,并定义其初值为100,然后输出该变量的值,在下面写出修改后的代码片段。
public class Variable {
public static void main(String[] args) {
int a=10;
String b=“10”;
System.out.println(“b=”+b); }
}
3、修改上述程序,定义一个字符型变量,并定义其初值为65536,然后输出该变量的值,在下面写出修改后的代码片段,如果有错误,说明原因。
public class Variable {
public static void main(String[] args) {
char a=65536;
System.out.println(“a=”+(int)a);
}
}
4、执行下述程序,写出程序运行结果
public class Test
{
public static void main(String args[])
{
boolean x,y,z;
int a=12,b=24;
x=(a>b);
y=(a!=b);
z=(a+b==36);
System.out.println(“x=”+x);
System.out.println(“y=”+y);
System.out.println(“z=”+z);
}
}
运行结果:
x=false
y=true
z=true;
5、编写Java应用程序完成以下功能:计算10秒钟后物体的位置。输出位置以米为单位,计算的数学公式为:x(t) = 0.5 × gt2 + vit + xi 0.59.811010+010+0=490.5米
变量 作用 值
g 加速度 (m/s2) 9.81
t 时间 (s) 10
vi 初速度 (m/s) 0
xi 初始位置 0
public class Test {
public static void main(String[] args) {
double g=9.81;
int t=10;
int vi=0;
int xi=0;
double x;
x=0.5*g*t*t+vi*t+xi;
System.out.println(x);
}
}
6、编写Java应用程序找出所有水仙花数。水仙花数是三位数,它的各位数字的立方和等于这个三位数本身,例如:371=333+777+111,371就是一个水仙花数。
public class Test {
public static void main(String[] args) {
int a,b,c;
for(int i=100;i<999;i++) {
a=i%10;
b=(i/10)%10;
c=i/100;
if(a*a*a+b*b*b+c*c*c==i) {
System.out.println(i);
}
}
}