java 语言包含的运算 :算术运算,赋值运算符,比较运算,逻辑运算,位运算,类型相关运算。
1.算术运算:java支持的算术运算包括加减乘除运算,求余运算以及自加或自减运算。
public class Div { public static void main (String [] args) { double a = 3.14; int b = 16; double c= a+b; System.out.println(c); System.out.println(a-b); System.out.println(a*b); System.out.println(a/b); System.out.println(a/0.0); System.out.println(a/0); System.out.println(a%b); System.out.println(a%0); double d = a++ +2; double e = ++a + 2; System.out.println("d="+d+‘ ‘+"e="+e); } }
java并没有提供乘方和开方运算,可以通过java.lang.Math类工具来计算
public class math { public static void main (String [] args) { double a = 3.2; double b = Math.pow(a,5); System.out.println(b); double c = Math.sqrt(a); double d = Math.random(); System.out.println(d); double e = Math.sin(a); System.out.println(e); } }
负号可以用来做求负运算
public class qiufu { public static void main (String [] args) { int x = -5; x = -x; System.out.println(x); } }
时间: 2024-11-05 22:39:52