基础程序设计02

数值操作符

数值操作符包括:加号+、减号-、乘号*,除号/,求余%。

幂运算

Math.pow(a,b)
pow方法定义在Math的类中,代表a的b次方。

public class practice{
public static void main(String args[]){
//求2的5次方
System.out.println("2的5次方是 "+Math.pow(2,5));
	}
}

运行结果:
在这里插入图片描述

数值操作符的优先级

首先是乘法,除法和求余运算,如果这些都存在若干个那么就按照从左到右的顺序。最后是加法和减法。如果用()将算是括起来可以提升优先级。

增强赋值操作符

+=,-=,*=,%=,/=
例子: a+=1;等同于a=a+1;

自增自减操作符

++和–
++a是指a先加1,然后带入运算
a++是指先参与运算,等到下一次在进行a+1以后的值的运算。

数值类型的转换

数值的转换方式是在原有的形式前加上(转换后的类型)。

查看当前时间

显示当前时间System.currentTimeMillis();返回当前的时间。但是这个方法并不是直接返回当前确切的时分秒,而是从1970年1月1日0:00开始一直到现在的毫秒。(1秒=1000毫秒)

public class practice{
	public static void main(String args[]){
	long totalseconds=(System.currentTimeMillis())/1000;
	long seconds=totalseconds%60;
	long  totalminutes=totalseconds/60;
	long minutes=totalminutes%60;
	long totalhours=totalminutes/60;
	long hours=totalhours%24;
	System.out.println("current time is "+hours+"时"+minutes+"分"+seconds+"秒");
			}
}
//打印的时间为格林尼治的时间如果要打印中国的北京时间需要在小时前加8.

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44586668/article/details/88190388