Java数据类型和表达式

一、基础实验内容
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);
		}
	}

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值